Link to home
Start Free TrialLog in
Avatar of yuvaprakash
yuvaprakash

asked on

Binary -> Hex & Octal & Decimal & char printing

Hi everyone,
I am new to embed area and stucking up with this for the past 1 day..finally i decided to ask u guys..

I am having a bit array like this

unsigned char bitArray[8]={'0','1','0','0','0','0','0','1'};

by calling a function, i need to print the above in the following formats..

1. hex
2.Octal,
3.char (most impt.)
4.decimal

can u plz help me in this..!

Avatar of Fisman
Fisman

Depends on the libraries you can use ... I assume you have string.c.

unsigned char bitArray[8]={'0','1','0','0','0','0','0','1'};

unsigned char tmp=0;
char i;

// Get the bits in a byte
for (i=0; i<8; i++)
{
  tmp += bitArray[i];
  tmp <<= 1;
}

printf("%0.2x", tmp);   // print as HEX
printf("%d", tmp;        // print as int
printf("%0.2o", tmp);  // print as octal
printf("%c", tmp);       // print as char

If you do not have printf you have to specify what you mean by "printing" as a character or other value ...
Sorry, that seems a bit incomplete at second glance ...

You need to stick above in a function of course, I neglected to that but it is trivial.

Also you might need stdio or something for the printf's and the string library has sprintf which will allow you to print to strings (character arrays), which may be more in line with what you need.

If you look at the char data type, the data in tmp above will already be a char, which can then be converted to whatever format you need.

Of course in your example your bit array had char's for 0 and 1 (0x30 and 0x31) I assume this was a "mistake" and you will be using values 0 and 1. If not you can either subtract 0x30 from the value in your array or add 1 if bitArray[i] == '1' only.



Avatar of yuvaprakash

ASKER

thx fisman,

but instead of printing 41(decimal) its printing 34..
i tried it failed..

thx agn for ur effort..

ops..after i posted my comment i got ur second reply..!..i ll check it out

fisman,
u meant like this..

unsigned char bitArray[8]={0,1,0,0,0,0,0,1};
a small logical change in ur prgm

for (i=0; i<8; i++)
{
  tmp += bitArray[i];
  îf (i<7)
      tmp <<= 1;
}

we got to avoid the last shift..

anyhow thx a lot for ur idea..!

// Get the bits in a byte
for (i=0, tmp = 0; i<8; i++) tmp = tmp << 1 + bitArray[i];

Paul
Or, for:

unsigned char bitArray[8]={'0','1','0','0','0','0','0','1'};

use:

for (i=0, tmp = 0; i<8; i++) tmp = (tmp << 1) + (bitArray[i] - '0');

Paul
Avatar of Julian Hansen
Another option would be (just for interest

unsigned char byte = 0 ;
unsigned char bitarray[8] ={0,1,0,0,0,0,0,1};
for (int i=7;i >= 0;i--) byte |= bitarray[i] << (7 - i) ;

Or
unsigned char bitarray[8] ={'0','1','0','0','0','0','0','1'};
for (int i=7;i >= 0;i--)  byte |= (bitarray[i] - '0') << (7 - i) ;

ASKER CERTIFIED SOLUTION
Avatar of Fisman
Fisman

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial