Cast uint8_t-array to readable format
I have this code, which receives a row of byte-values, inserted in an array. How can I show the content "readable" and not like this: R$⸮⸮>⸮⸮⸮
Here's the relevant code:
// Read data from UART.
uint8_t myData[8];
String myDataString = "";
int length = 0;
ESP_ERROR_CHECK(uart_get_buffered_data_len(uart_num, (size_t*)&length));
length = uart_read_bytes(uart_num, myData, length, 100);
if (length > 0) {
for (int i=0; i<length; i++) {
//SerialMon.write(myData[i]);
myDataString += myData[i]+",";
}
SerialMon.println(myDataString); // should be something like "82,36,18,43,129,255,255,255"
}
I believe the problem is, that a byte value like 82 in ASCII corresponds to the letter 'R' when displaying it as a character. Is there an easy way to convert the values, so that an element like 82 will show up as "82" instead... or even better: as HEX?
You are correct with your assumption that myData[i]
gets interpreted as ASCII-code, but you can easily convert it into a HEX-string as follows:
myDataString += String(myData[i], HEX);