View previous topic :: View next topic |
Author |
Message |
denin_online
Joined: 22 Nov 2006 Posts: 1
|
show bits in a printf??? |
Posted: Thu Dec 21, 2006 7:53 pm |
|
|
Hello
I�m trying to show bytes in the printf, is there some format that I can show the bites of a valor.
Like to show decimal is � %d � and hex is � %x� �.
I would be grateful if someone helps me.
Thanks,
Denin_online |
|
|
jecottrell
Joined: 16 Jan 2005 Posts: 559 Location: Tucson, AZ
|
|
|
Guest
|
|
Posted: Fri Dec 22, 2006 5:10 am |
|
|
here is Bob version its for 16bits but you can change it for 8bits easy
Code: |
///////////////////////////////////////////////////////
/// coverts the final value to binary 16bits
void PrintAsBinary ( int16 value )
{ char display[16];
int i;
int binaryValue;
for ( i = 0; i < LENGTH_OF_SHORT; i++ )
{
/* Check MSB against 1 */
if ( 0x8000 == ( value & 0x8000 ) )
{
binaryValue = 1;
display[i]='1';
}
else
{
binaryValue = 0;
display[i]='x';
}
/* Print the binary value */
printf(lcd_putc,"%c",display[i] );
/* Shift next bit into mask position */
value = value << 1;
}
}
|
|
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Fri Dec 22, 2006 10:04 am |
|
|
I like my version below as it is the most optimized version I've seen and it is very easy to adapt to other variable sizes (only requires changing the int16 to int8 or int32).
Code: | ///////////////////////////////////////////////////////
// Prints the given value as binary.
void PrintAsBinary(int16 Value)
{
int8 i;
i = sizeof(Value) * 8; // Get number of bits.
do
{
// Check MSB against 1 and print the value.
if (shift_right(&Value, sizeof(Value), 0) == 1)
putc('1');
else
putc('0');
} while (--i);
} |
|
|
|
|