# convert a binary into decimal

Discussion in 'C Programming' started by QQ, Mar 22, 2006.

1. ### QQGuest

Hello

unsigned char a;

the a[0-3] represents a 4-bit binary,
a[4-6] represents another 3-bit binary.

I'd like to convert a[0-3] into a decimal.
For instance if {a[3] a[2] a[1] a[0}}= {0 1 0 1] the decimal should be
5

My program is like

if((a && 0x00)==1) return 0;
else if (a&& 0x01)==1) return 1;
.....

Is there any simpler way for it?

Thanks a lot!

QQ, Mar 22, 2006

2. ### =?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=Guest

QQ wrote:
> Hello
>
> unsigned char a;
>
> the a[0-3] represents a 4-bit binary,

return a & 0xf;

> a[4-6] represents another 3-bit binary.

return (a >> 4) & 0x7;

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=, Mar 22, 2006

3. ### pemoGuest

QQ wrote:
> Hello
>
> unsigned char a;
>
> the a[0-3] represents a 4-bit binary,
> a[4-6] represents another 3-bit binary.
>
> I'd like to convert a[0-3] into a decimal.
> For instance if {a[3] a[2] a[1] a[0}}= {0 1 0 1] the decimal should
> be 5
>
> My program is like
>
> if((a && 0x00)==1) return 0;
> else if (a&& 0x01)==1) return 1;
> ....
>
> Is there any simpler way for it?
>
> Thanks a lot!

0-3 = a & 0x0F;
4-6 = (a & 0x70) >> 4;

--
==============
Not a pedant
==============

pemo, Mar 22, 2006
4. ### Martin AmbuhlGuest

QQ wrote:
> unsigned char a;

> the a[0-3] represents a 4-bit binary,
> a[4-6] represents another 3-bit binary.

> I'd like to convert a[0-3] into a decimal.
> For instance if {a[3] a[2] a[1] a[0}}= {0 1 0 1] the decimal should be
> 5
>
> My program is like
>
> if((a && 0x00)==1) return 0;
> else if (a&& 0x01)==1) return 1;

I don't understand how the above can do what you want.

> Is there any simpler way for it?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <limits.h>

int main(void)
{
unsigned char a;
int i;
srand(time(0));
for (i = 0; i < 4; i++) {
a = (unsigned) rand();
printf
("a = %3u (%#03o, %#02x), first 4 bits=%u,"
" next 3 bits=%u\n",
a, a, a, a >> (CHAR_BIT - 4), 07 & (a >> (CHAR_BIT - 7)));
}
return 0;
}

a = 186 (0272, 0xba), first 4 bits=11, next 3 bits=5
a = 171 (0253, 0xab), first 4 bits=10, next 3 bits=5
a = 19 (023, 0x13), first 4 bits=1, next 3 bits=1
a = 98 (0142, 0x62), first 4 bits=6, next 3 bits=1

Martin Ambuhl, Mar 22, 2006
5. ### Barry SchwarzGuest

On 22 Mar 2006 09:45:53 -0800, "QQ" <> wrote:

>Hello
>
>unsigned char a;
>
>the a[0-3] represents a 4-bit binary,
>a[4-6] represents another 3-bit binary.
>
>I'd like to convert a[0-3] into a decimal.
>For instance if {a[3] a[2] a[1] a[0}}= {0 1 0 1] the decimal should be
>5
>
>My program is like
>
>if((a && 0x00)==1) return 0;

The if is guaranteed to always be false. Anything && 0 is always
false.

>else if (a&& 0x01)==1) return 1;

This is no different than if(a). Did you perhaps mean (a & 0x01)?

Apparently your unsigned char holds 0 or 1 as opposed to '0' and '1'.
In this case, you can compute
num1 = a[3]*8 + a[2]*4 + a[1]*2 + a[0];
and
num2 = a[6]*4 + a[5]*2 + a[4];

Remove del for email

Barry Schwarz, Mar 23, 2006