I want to accept a uint8_t vector
into a function, which every element is then converted to a 2-digit (all digits not used are 0) written hexadecimal stored in a string. I think I might be getting confused here...
I figured a way to do it with printf (I think):
std::vector<uint8_t> name1 = { 's', '2', '9', '2', 'a', 'b' };
for (int i = 0; i < name1.size(); i++) {
printf("printf: %002X ", name1[i]);
std::cout << std::endl;
}
But then the idea is that I create a string such as:
MSGPACK 733239326162
where 73 is s, 32 is 2 obviously etc...
So I want to put the above MSGPACK...162 into a variable which stores it and then later do something like printf(msgpack1)
.
I tried the following way...
char buffer[40];
std::vector<uint8_t> name1 = { 's', '2', '9', '2', 'a', 'b' };
for (int i = 0; i < name1.size(); i++) {
snprintf(buffer, sizeof(buffer), "%002X", name1[i]);
}
buffer[name1.size()] = '\0';
so I was hoping that I could print it all out like this:
printf("MSGPACK %s", buffer);
I'm not sure what I'm doing wrong here, but I'm getting MSGPACK 62
as output instead. How to get what I need?
Edit: I see that buffer is overwritten now with the last member of vector, but I can't place just buffer[i]
into snprintf
, so what would be the way to go about it?