I am using C++ on Arduino.
Suppose I have a stream of binary data;
binary data: 0xFF, 0x00, 0x01, 0xCC
I want to convert it to the ASCII equivalent and store it in a String object type.
The converted string should look like this "FF0001CC".
Here are some draft code.
char buffer[100];
String myString;
for (int i=0; i < numBytes; i++)
{
//assume buffer contains some binary data at this point
myString += String(buffer[i], HEX);
}
The problem with this code is that myString contains FF01CC, not FF0001CC.
#define hex_char(c) (((c)>9)?'A'+(c)-10:'0'+(c)); myString += hex_char((buffer[i]>>4)&0xf); myString += hex_char((buffer[i] & 0xf));or something like that...