2

I'm reading a RFID Card from RC522 and send the data to my computer. My problem is the conversion of byte[] to String or std::string.

#define SIZE_BUFFER     18
#define MAX_SIZE_BLOCK  16

byte buffer[SIZE_BUFFER] = {0};
byte tam = SIZE_BUFFER;

// get data
status = mfrc522.MIFARE_Read(bloco, buffer, &tam);

  Serial.print(F("\nDados bloco ["));
  Serial.print(F("]: "));

// printing...
 for (uint8_t i = 0; i < MAX_SIZE_BLOCK; i++)
  {
      Serial.write(buffer[i]);
      str += String(buffer[i]);
  }

OUTPUT:

Dados bloco []: asdas

So, it's ok! But if I try make a string:

  String str = "";

  for (uint8_t i = 0; i < MAX_SIZE_BLOCK; i++)
  {
      str += String(buffer[i]);
  }
  Serial.println(" ");
  Serial.println(str);

The output is:

97115100971153232323232323232323232

What is the correct way to create a String from byte[]?

3

byte and char are the same. If you set 0 as string terminator after last character in the buffer, you get a zero terminated string. If you really must use String, you can create an instance with a constructor that takes zero terminated string.

buffer[tam] = 0;
String str((char*) buffer);

Let in the buffer a place for the zero. (char*) is cast that says that the byte array is a char array.

Note: Don't use String class in a MCU, use zero terminated character arrays.

  • 1
    I just need the value, but String is easy to send via Bluetooth. I can send char too, but I don't know to convert. – Augusto Oct 30 '18 at 18:32
  • set 0 as string terminator after last character in the buffer. but let space for it. if the RFID numbers are 18 chars, then allocate 19 – Juraj Oct 30 '18 at 18:33
  • I enhanced the answer – Juraj Oct 30 '18 at 18:38
  • @Augusto, did it help? – Juraj Oct 31 '18 at 16:11
2

If you change str += String(buffer[i]); to str += (char)buffer[i];, it will print the ascii characters to the serial monitor.

The following sketch can be used to see the difference in binary compiled size using these two methods of "adding to the string".

// 4026 to 3758 bytes.
#define SIZE_BUFFER     18
#define MAX_SIZE_BLOCK  16

byte buffer[SIZE_BUFFER] = {
  0x61, 0x73, 0x64, 0x61,
  0x73, 0x20, 0x20, 0x20,
  0x20, 0x20, 0x20, 0x20,
  0x20, 0x20, 0x20, 0x20
};

String str = "";

void setup(){

  Serial.begin(9600);

  for (uint8_t i = 0; i < MAX_SIZE_BLOCK; i++)
  {
      // 4026 bytes. Prints: "97115100971153232323232323232323232"
      //str += String(buffer[i]);

      // 3758 bytes. Prints: "asdas           "
      str += (char)buffer[i];
  }
  Serial.println(" ");
  Serial.println(str);
}

void loop(){}

Casting each element in the byte buffer to Char, then adding it to the string drops the binary compiled size from 4026 to 3758 bytes. This is good, but we can really improve the sketch by not using the String object at all.

This sketch is the same as the first one, except it uses a Character Array instead of a String.

// 2074 bytes.
#define SIZE_BUFFER     18
#define MAX_SIZE_BLOCK  16

byte buffer[SIZE_BUFFER] = {
  0x61, 0x73, 0x64, 0x61,
  0x73, 0x20, 0x20, 0x20,
  0x20, 0x20, 0x20, 0x20,
  0x20, 0x20, 0x20, 0x20
};

// Use a char array instead of the String object.
char outputBuffer[SIZE_BUFFER];

void setup(){

  Serial.begin(9600);

  for (uint8_t i = 0; i < MAX_SIZE_BLOCK; i++)
  {
      // 2074 bytes. Prints: "asdas           "
      outputBuffer[i] = (char)buffer[i];
  }
  Serial.println(" ");
  Serial.println(outputBuffer);
}

void loop(){}

The binary compiled size is 2074 bytes. That's 1684 bytes less than using the String object.

There are many functions available to work with Character Arrays on an Arduino, such as http://www.cplusplus.com/reference/cstring/ and http://www.cplusplus.com/reference/cstdlib/.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.