Converting integer array to a single string

So, I got this sketch for reading RFID tags, printing the output to the serial port and it works fine.

Here is the part of the code that does the reading to throw it on the port:

Serial.println("Card Number:");
          Serial.print(rfid.serNum[0]);
          Serial.print(", ");
          Serial.print(rfid.serNum[1]);
          Serial.print(", ");
          Serial.print(rfid.serNum[2]);
          Serial.print(", ");
          Serial.print(rfid.serNum[3]);
          Serial.print(", ");
          Serial.print(rfid.serNum[4]);
          Serial.println(" ");

The output is something like 245, 220, 58, 230, 245

My question is whether and, if yes, how it is possible to grab all the 5 elements of this array and put it on a character string. The purpose of this conversion is to further throw it on a SQL query to check if this "serial number" is registered in the database. Seems like the type of rfid.serNum is a unsigned char, as I saw it in the library:

unsigned char serNum[5];

I appreciate any help! :smiley:

I use sprintf() to convert several variables into a character string, formatted just about any way one likes.
Some examples at sprintf | Liudr's Blog

jremington:
I use sprintf() to convert several variables into a character string, formatted just about any way one likes.
Some examples at sprintf | Liudr's Blog

Thanks for this tip, but how should I use it? I tried something like

char cardCode[16];
          sprintf(cardCode, %s%s%s%s%s, rfid.serNum[0], rfid.serNum[1], rfid.serNum[2], rfid.serNum[3], rfid.serNum[4]);

and it gives me an error:

RFID:50: error: expected primary-expression before '%' token
RFID:50: error: 's' was not declared in this scope

:0

potew:

jremington:
I use sprintf() to convert several variables into a character string, formatted just about any way one likes.
Some examples at sprintf | Liudr's Blog

Thanks for this tip, but how should I use it? I tried something like

char cardCode[16];

sprintf(cardCode, %s%s%s%s%s, rfid.serNum[0], rfid.serNum[1], rfid.serNum[2], rfid.serNum[3], rfid.serNum[4]);



and it gives me an error:

*RFID:50: error: expected primary-expression before '%' token*
*RFID:50: error: 's' was not declared in this scope*

enclose the format string in quotes. Did you even see the samples?

pepe:
Hi,

The char array supplied to receive the string must be large enough.

Five numbers of three figures, plus four comas, plus one ending nul character give a length of 5x3+4+1=20.

The code should be :

char cardCode[20];

sprintf(cardCode, "%u,%u,%u,%u,%u", rfid.serNum[0], rfid.serNum[1], rfid.serNum[2], rfid.serNum[3], rfid.serNum[4]);

I understand what you did, but I aint including the commas for the final variable. Thanks for the tip :slight_smile:

RobvdVeer:
:0
enclose the format string in quotes. Did you even see the samples?

No, and where can I get those samples?
Putting them into quotes gave no compilation errors this time... thanks, guys!!

enclose the format string in quotes. Did you even see the samples?
No, and where can I get those samples?

If you had taken the time to read BOTH lines of reply #1, you would have seen the samples.

jremington:

enclose the format string in quotes. Did you even see the samples?
No, and where can I get those samples?

If you had taken the time to read BOTH lines of reply #1, you would have seen the samples.

I really haven't noticed the link on your first reply. My mistake :blush:

pepe:
If you don't include separators such as comas, be careful not to get confusing results.

For instance, how can one make a difference between { 12, 3, 4, 5, 67 } and { 1, 234, 5, 6, 7 }, since the result would be "1234567" in both cases ?

I also thought on that issue. The solution I found (on the link jremington posted) is to write "%003u%003u....." in order to add one or two zeroes whenever the number is smaller than 100, while it reserves the 3-digit limit (:
Thanks again, if it weren't for your help, I'll be stucked until now :blush:

HI

if i want to use the same concept on client object of WiFiClient library of MKR1000 how do i proceed. since WiFiClient has no member named sprintf?

sprintf() is standard C/C++.