Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert Binary Data to Hex

Status
Not open for further replies.

Stripey

MIS
Sep 19, 2005
3
ES
How can I convert a buffer of binary data (unsigned char) to hex for displaying?

Stripey
 
well, first off, if the character buffer is NOT in base 10 you will need to use strtol to convert it.

i.e. if your buffer looks like "10010010011110" you would do

char* buffer[] = "10010010011110";
char* endPtr;
int val = strtol(buffer,endPtr,2); // 2 is the base

if you just want to print the output in hex format, sprintf or CString::Format with a %x will do that for you.

Matt

 
I doubt if the question was what Matt meant.
May be simply
for(ii=0; ii<buf_size; ii++) printf(&quot;%02x&quot;, buf[ii]);

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top