Hexadecimal to Bit Display Utility
2011/11/04 Leave a comment
This is a very simple command-line utility to display 32bit hexadecimal numbers to a friendly bit-to-bit visualization, with a nice indication of bit offsets.
I found this really useful when decoding dumps of 32 bit registers against the register descriptions found in datasheets at 2 AM in the morning after a day spent watching boot logs on a terminal… this things can happen!
This is an example of the application call and output:
$ ./hex2bit deadbeef cafecafe 12345678
bin: 3 2 1 0
1098 7654 3210 9876 - 5432 1098 7654 3210
0xdeadbeef: 1101 1110 1010 1101 - 1011 1110 1110 1111
0xcafecafe: 1100 1010 1111 1110 - 1100 1010 1111 1110
0x12345678: 0001 0010 0011 0100 - 0101 0110 0111 1000
Please note that the 0x prefix on input arguments is optional.
Of course the application is really simple, just compile it with
gcc -o hex2bit hex2bit.c
and you are ready to go!
Source code:
#include <stdio.h>
int main (int argc, char ** argv)
{
int in;
int i;
int j;
printf(" bin: 3 2 1 0\n"\
" 1098 7654 3210 9876 - 5432 1098 7654 3210\n"
"\n");
for (j = 1; j < argc; j++) {
sscanf(argv[j], "%x", &in);
printf("0x%08x: ", in);
for (i = 31; i >= 0; i--) {
if (in & (1 << i))
putchar('1');
else
putchar('0');
if (!(i % 4))
putchar(' ');
if (i == 16)
printf("- ");
}
printf("\n");
}
return 0;
}
