converting byte array into string

Hello,
im new to this and have totaly no idea what im actually doin. I thought it couldnt be that hard to gather some premade code and change it just a little to fit into my project. I failed… All i wanna do is to use multiple RFID Readers to detect multiple RFID Tags at the same time. All i want to do is :read the IDs and save them into different String variables, not printing them on the screen. To manage this i took this premade function :

void dump_byte_array(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], HEX);
  }
}

wich deliveres results like “C93B9359” and modified it like this :

String dump_byte_array(byte *buffer, byte bufferSize) {
    String str = "";
    for (byte i = 0; i < bufferSize; i++) {
      if (buffer[i] < 0x10){
         str = str + " 0" + (buffer[i],HEX);
      }
      else
        str = str + (buffer[i],HEX);  
  }
  Serial.println(str);           //<- just for debug
  return str;
}

But now the result is “16161616”. Looks like i cannot do it this way… i know i should maybe learn the basics first before im doin things like this but i only need to know this little thing. I have been looking for days to a solution but i havent found one. :’( Please help

wich deliveres results like “C93B9359”

Look carefully at the code you posted. Does it look like it does in the IDE or have the array indices disappeared because the forum software interpreted them as indicators of italic text ?

Please edit your post and replace the quote tags with code tags as described in read this before posting a programming question

String dump_byte_array(byte *buffer, byte bufferSize) {
    String str = "";
    for (byte i = 0; i < bufferSize[i]; i++) {
      if (buffer[b][i][/b] < 0x10){
         str = str + " 0" + (buffer[i],HEX);
      }
      else
        str = str + (buffer[i],HEX);   
    }
  Serial.println(str);           //<- just for debug
  return str;
    for (byte i = 0; i < bufferSize[i]; i++) {
      if (buffer[b][i][/b] < 0x10){

Are you sure about those 2 lines ?

UKHeliBob:
Look carefully at the code you posted. Does it look like it does in the IDE or have the array indices disappeared because the forum software interpreted them as indicators of italic text ?

It looks like in the IDE.
The output via serial monitor looks like : C9 3B 93 59

UKHeliBob:
Please edit your post and replace the quote tags with code tags as described in read this before posting a programming question

Done

It looks like in the IDE.

It does now you have edited the original post and replaced the quote tags with code tags

Ok but how can i manage to save those HEX IDs like C9 3B 93 59 somehow to compare them later?

For an answer to the original question:

The long but easier to understand version:

String dump_byte_array(byte *buffer, byte bufferSize) {
  String str = "";
  for (byte i = 0; i < bufferSize; i++) {
    if (((buffer[i] & 0xF0) >> 4) <= 9) {
      str = str + (char)(((buffer[i] & 0xF0) >> 4) + '0'); //convert HEX 0-9 to ASCII
    } else {
      str = str + (char)(((buffer[i] & 0xF0) >> 4) + 'A' - 10); //convert HEX A-F to ASCII
    }
    if ((buffer[i] & 0x0F) <= 9) {
      str = str + (char)((buffer[i] & 0x0F) + '0'); //convert HEX 0-9 to ASCII
    } else {
      str = str + (char)((buffer[i] & 0x0F) + 'A' - 10); //convert HEX A-F to ASCII
    }
    //if (i < (bufferSize - 1)) str = str + ' '; //uncomment if you want a space between each HEX number
  }
  Serial.println(str);           //<- just for debug
  return str;
}

Shorter but more cryptic, to the compiler it is the same thing:

String dump_byte_array(byte *buffer, byte bufferSize) {
  String str = "";
  for (byte i = 0; i < bufferSize; i++) {
    str = str + ((((buffer[i] & 0xF0) >> 4) <= 9) ? (char)(((buffer[i] & 0xF0) >> 4) + '0') : (char)(((buffer[i] & 0xF0) >> 4) + 'A' - 10));
    str = str + (((buffer[i] & 0x0F) <= 9) ? (char)((buffer[i] & 0x0F) + '0') : (char)((buffer[i] & 0x0F) + 'A' - 10));
    //if (i < (bufferSize - 1)) str = str + ' '; //uncomment if you want a space between each HEX number
  }
  Serial.println(str);           //<- just for debug
  return str;
}

Ruffgear:
Ok but how can i manage to save those HEX IDs like C9 3B 93 59 somehow to compare them later?

Are the IDs always four HEX numbers? If so, combine them into an unsigned long (4-byte unsigned integer), or save as an array of four bytes. Converting to a String and saving as ASCII characters is wasteful of storage space, and the use of String variables can lead to problems because of the poor memory management on an arduino.

Ruffgear:
Ok but how can i manage to save those HEX IDs like C9 3B 93 59 somehow to compare them later?

Why do you need to convert them into HEX and even if you do then why are you trying to convert them into a String ?
What are you going to compare them with exactly ?

I assume you are playing around with a RFID RC522 and most will use miguel balboa RFID library

you can read the Unique ID of your tag through the PICC_ReadCardSerial() method call, which initializes a structure (defined here)

// A struct used for passing the UID of a PICC.
	typedef struct {
		byte		size;			// Number of bytes in the UID. 4, 7 or 10.
		byte		uidByte[10];
		byte		sak;			// The SAK (Select acknowledge) byte returned from the PICC after successful selection.
	} Uid;

and there is a uid variable of that type

// Member variables
Uid uid;  // Used by PICC_ReadCardSerial().

This is the typical approach:

MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
...
void loop()
{
  if ( ! rfid.PICC_IsNewCardPresent()) return;
  if ( ! rfid.PICC_ReadCardSerial()) return;
  // here rfid.uid.uidByte[] holds the bytes of your ID
...

As the ID is in rfid.uid.uidByte which is an array of bytes, you can use a [url=http://www.cplusplus.com/reference/cstring/memcmp/]memcmp()[/url] call to compare those bytes with a reference array. for example assume your IDs are on 4 bytes you can define a known card as

const byte secretCard[] = {0xDE, 0xAD, 0xBE, 0xEF};

and when you want to compare the result of the RFID scan with this you would just do a

if (memcmp (secretCard, rfid.uid.uidByte, 4) == 0) {
  // we have a match!
} else {
  // No Match
}

there is no reason to generate a String representation

It is working now, thank you all for your hints and help. I slightly begin understanding what is going on, but thats enough for me. You are awesome :slight_smile: