Saturday, April 20, 2013

To print the buffer in HEX

Nothing big, thought I will add this for my own reference. This will print the buffer in hex.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
static void
print_buffer(unsigned *buffer, int len)
{
   unsigned i = 0;
   for(i=0;i<len;i++){
      if(!(i%8)) 
          printf("\n ");
      printf(" 0x%02x",buffer[i]);
   }
   return;
}

You should get an output something like this based on your input
1
2
3
  0x74 0x65 0x73 0x74 0x20 0x6c 0x61 0x62
  0x65 0x6c 0xa0 0xba 0x9f 0x93 0x6c 0xda
  0x31 0x18 0x27 0xa6 0xf7 0x96 0xff 0xd5

Hope this helps someone. 

No comments:

Post a Comment