Reputation: 2134
I have the task print an array of uint8_t
in one line and using the given log function which is print each message in one line. So, I think I must merge every element in a string and print. I try to use std::stringstream
uint8_t array[512];
std::stringstream ss;
for (int i = 0; i < array_len; i++)
{
ss << "0x" << std::hex << array[i] << ", ";
}
log_given_print("%s", ss.str().c_str()); // in fact you can replace by printf
It seems to work well. But today, I get the strange log:
0xETX, 0xSOH, 0x%, 0xN, 0x
I checked the ascii table https://www.asciitable.com/ and see that it is some bytes like 0x01, 0x03. I think the problem is %s
, normally, I used %x
to print each element of array.
How can I fix this problem?
Upvotes: 0
Views: 704
Reputation: 409176
When using the formatted output operator <<
, it will print characters as characters. You need to convert the values to e.g. unsigned
to be able to print its actual integer value:
ss << "0x" << std::hex << static_cast<unsigned>(array[i]) << ", ";
Upvotes: 1