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[]
?
2 Answers 2
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.
-
1I 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– Augusto2018年10月30日 18:32:14 +00:00Commented Oct 30, 2018 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 192018年10月30日 18:33:07 +00:00Commented Oct 30, 2018 at 18:33
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/.
Explore related questions
See similar questions with these tags.