[techtalk] Does anybody know an option for od to do binary?

Samantha Jo Moore sjmoore at TheTahoeGroup.com
Wed Oct 6 10:30:07 EST 1999


Clare,

> I need to read the bits (not bytes) in a file. While the od -h will do hex I haven't 
> found anything to let me traverse through the bits. i.e. the first 14 bits of field 3 
> are the sequence number and the last 2 are something else.
> Any help woudl be greatly appreciated.

Hex is the easiest representation of binary.  It is very easy to read if you know
how the hex digits are structured:

  0 - 0000
  1 - 0001
  2 - 0010
  3 - 0011
  4 - 0100
  5 - 0101
  6 - 0110
  7 - 0111
  8 - 1000
  9 - 1001
  A - 1010
  B - 1011
  C - 1100
  D - 1101
  E - 1110
  F - 1111

You can also make a little C program to translate every hex digit you read into the
equivalent binary digits.

#include <stdio.h>
main()
{
	unsigned int	y;

	while ((x = getchar()) != EOF)
	{
		switch (x)
		{
			case '0':	printf ("0000"); break;
			case '1':	printf ("0001"); break;
			case '2':	printf ("0010"); break;
			case '3':	printf ("0011"); break;
			case '4':	printf ("0100"); break;
			case '5':	printf ("0101"); break;
			case '6':	printf ("0111"); break;
			case '7':	printf ("1111"); break;
			case '8':	printf ("1000"); break;
			case '9':	printf ("1001"); break;
			case 'A':	printf ("1010"); break;
			case 'B':	printf ("1011"); break;
			case 'C':	printf ("1100"); break;
			case 'D':	printf ("1101"); break;
			case 'E':	printf ("1110"); break;
			case 'F':	printf ("1111"); break;
			default:	printf ("%c", x); break;
		}
	}
}

Samantha Jo Moore
CTO - The Tahoe Group, Inc.
http://www.thetahoegroup.com
sjmoore at thetahoegroup.com

************
techtalk at linuxchix.org   http://www.linuxchix.org




More information about the Techtalk mailing list