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?
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);
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
What about mutiple hex numbers?
Like:
char bob[3] = {0x82,0xF4,0xED};
Last edited by azjherben; 08-27-2009 at 02:36 PM.
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] << ' ';
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
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.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.