|
-
June 2nd, 2008, 01:21 PM
#1
Converting char to a string of hex bytes
I might have got some of the terminology wrong here I am very new to C++, and find the data types very confusing.
My app receives data from a socket using recv(), and the data ends up in char "line". I would like to convert "line" to a string of hex values (as strings, not as actual hex bytes) corresponding to the characters within it. I have spent most of the day Googling this and trying every way I can think of, and this is the best I can come up with. Assume that in the data received, line[1] is 0x00, line[2] is 0xBB, line[3] is 0x80 and line[4] is 0x8B.
Code:
ostringstream oss;
oss << hex << static_cast<int>( line[1] ) << static_cast<int>( line[2] ) << static_cast<int>( line[3] ) << static_cast<int>( line[4] );
string s = oss.str();
This works, but the output is ugly - something like "0ffffffbbffffff80ffffff8b". I would like the result to be something like "00BB808B".
Hope this makes sense!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|