I'm getting inoput from a binary file, in hex.
And I want it to say the first nine characters... like "0x05 0x53,0x84 " ... and so on. So how do I turn a char in hex to a string in text?
Printable View
I'm getting inoput from a binary file, in hex.
And I want it to say the first nine characters... like "0x05 0x53,0x84 " ... and so on. So how do I turn a char in hex to a string in text?
Well, it's a bit ugly, but:
There may be an easier way. Of course, you could always use the C functions for this.Code:#include <iostream>
#include <iomanip>
int main() {
char c = 0x05;
std::cout << "0x" << std::setw(2) << std::setfill('0') << std::hex << (int)c << std::endl;
return 0;
}
Code:printf("0x%02x\n", c);
What about mutiple hex numbers?
Like:
char bob[3] = {0x82,0xF4,0xED};
Well, you just do it in a loop! Repeated executing
should do what you need.Code:std::cout << "0x" << std::setw(2) << std::setfill('0') << std::hex << (int)bob[x] << ' ';
What if I can't use cout.
If you need to use C++, but want to generate a std::string to use later, you can use stringstreams. See the FAQ for details.