Click here to Skip to main content
15,904,497 members
Articles / Programming Languages / C++

Binary array to printable hex string in C

Rate me:
Please Sign up or sign in to vote.
1.24/5 (7 votes)
1 Jul 2007CPOL1 min read 44.3K   247   13   3
Tracing contents of a packet.


This is generic function to trace out the contents of a packet (binary array).


I needed to trace the contents of a packet and was looking for a piece of code to quickly trace out the contents of the packet. I wasn't able to find one on the net. So I wrote a simple function which prints out the binary blob in HEX string as you see it in WinDbg. Thought it would be a good idea to share out with others who might need it.

Using the code

Just include the trace function as it is. Instead of the AllocateMemory function, which is my own alloc function, use any memory alloc you prefer. If your memory alloc function doesn't initialize the contents of the memory block returned, it would be a good idea to initialize it to 0 (mine initialises to 0). Instead of printf, you can substitute it with any other trace function which your app might be using. Most of the apps dealing with network packets don't have a console to print to; rather they spew the data on to a debugger or have some kind of tracing.

///function to trace out a byte arr
DWORD TraceOut(BYTE* pConfigOut,DWORD dwSizeOfConfigOut)
    DWORD retCode = 0;
    DWORD dwSizeOfArr = (2 * dwSizeOfConfigOut) + 1;
    CHAR* pszCharArr = NULL;

    DWORD i = 0;
    retCode = AllocateMemory(dwSizeOfArr, (PVOID*)&pszCharArr);
    if (retCode != ERROR_SUCCESS)
        goto cleanUp;
    DWORD j =0;
    for(i=0; i<dwSizeOfConfigOut ; i++)
        char ch = pConfigOut[i];
        char ch0 = (ch>>4) & 0x0F;
        char ch1 = ch & 0x0F;
        if((0<= ch0) && (ch0 <= 9))
            pszCharArr[j] = '0' + ch0;
            pszCharArr[j] = 'A' + (ch0 - 10);
        if((0<= ch1) && (ch1 <= 9))
            pszCharArr[j] = '0' + ch1;
            pszCharArr[j] = 'A' + (ch1 - 10);
    pszCharArr[j] = '\0';

    printf("Binary array is %s", pszCharArr);
    return retCode;

Points of Interest

So if the binary blob is like this in (bits):

0000 1111 1111 1111 1110 1100 0000 0000 0000 0001

which in hex string would be:

0F FF  EC 00 01

and you want to trace it out as it is.... you can read one character at a time from the binary blob and store it in a char variable. Then extract out the left most four bits which represent one symbol of HEX, and then extract the right most four bits which represent the other HEX symbol.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Web Developer
United States United States
If only I knew the purpose of my life, I would see the justification in me doing what I do. What the heck, I could'n care less about every random occerance to have a purpose else Gauss and Laplace wouldn't have been legends. As for me I am a mere mortal who among other things, enjoy's killing bugs and I guess my passionate animosity towards bugs is what helps me in doing whatever I do somewhere in the NorthWest. (Don't you guys make up any ideas about too many bugs in anything that comes out of NorthWest). Just be aware, it could be worse, if not betterSmile | :) .
And I live my life happily there after.

Comments and Discussions

QuestionHow about... Pin
Wokaiie1-Jul-07 18:05
Wokaiie1-Jul-07 18:05 
QuestionConsidered using sprintf_s? Pin
KenThompson1-Jul-07 13:36
KenThompson1-Jul-07 13:36 
You go through a lot of work to produce HEX characters. Have you considered:

void PrettyPrint( char * buf, int size )
  std::string str;
  char tmp[6];
  for( int x = 0; x < size; x++ )
    sprintf_s(tmp, sizeof(tmp), "0x%02x ", buf[x]);
  printf("HEX Bytes = %s\r\n", str.c_str() );

Not a dig, just curious if you have a method for your code.

AnswerRe: Considered using sprintf_s? Pin
samal_recw952-Jul-07 10:44
samal_recw952-Jul-07 10:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.