Here is one way to implement displaying a byte in binary form :
typedef unsigned char uchar;
void PrintByteInBinary( uchar byte )
{
uchar mask = 0x080; int n;
for( n = 0; n < 8; ++n )
{
printf( "%s", byte & mask ? "1" : "0" );
mask >>= 1;
}
}
You can pass in the individual bytes of any type and size of data. Here is a way to use a union so you can easily extract the bytes :
typedef union
{
uchar bytes[8];
int intvalue;
short shortvalue;
float floatvalue;
double doublevalue;
} ByteUnion;
If you set the intvalue to an integer value then you can pass the individual bytes to the print function to display them. Here's a function that will print as many bytes as you have and a sample of using it.
void PrintBytesInBinary( uchar *bytes, int byteCount )
{
int n;
for( n = 0; n < byteCount; ++n )
PrintByteInBinary( bytes[n] );
}
void PrintValues( void )
{
ByteUnion bu = { 0 };
bu.intvalue = 42;
PrintBytesInBinary( bu.bytes, sizeof( int ) );
bu.shortvalue = 96;
PrintBytesInBinary( bu.bytes, sizeof( short ) );
bu.floatvalue = 3.1415f;
PrintBytesInBinary( bu.bytes, sizeof( float ) );
bu.doublevalue = 1.4142;
PrintBytesInBinary( bu.bytes, sizeof( double ) );
}
I have not tested this code but it should be close to functional. Good luck.