Velocity Reviews > Question converting unsigned char [] to int

# Question converting unsigned char [] to int

No Such Luck
Guest
Posts: n/a

 06-17-2005
Hi all:

I have an unsigned char array (size 4):

unsigned char array[4];

array[0] = 0x00;
array[1] = 0x00;
array[2] = 0x02;
array[3] = 0xe7;

I would like to convert array[] to an integer.

0x00 0x00 0x02 0xe7 = 00000000 00000000 00000010 11100111 = 743

Any help is appreciated.

yatindran@gmail.com
Guest
Posts: n/a

 06-17-2005
hi
first u need to understand the relationship between BCD representation.
like 3 is represented as 0111 and not 111. 4 will be considered as 0100
and not 100.so if u r given a number , say 0xe7, which means
11100111,shift it to right by 4.

int temp = array[3] >> 4;
which means
temp = 00001110 (= 0x0e)
int temp2 = array[3] << 4;
temp2 = 01110000 (0x70)
similarly u can split up all the elements by shifting and get the 4 bit
equivalents. convert them to get the exact BCD no.
hope that was of some help.

Me
Guest
Posts: n/a

 06-17-2005
> I have an unsigned char array (size 4):
>
> unsigned char array[4];
>
> array[0] = 0x00;
> array[1] = 0x00;
> array[2] = 0x02;
> array[3] = 0xe7;
>
> I would like to convert array[] to an integer.
>
> 0x00 0x00 0x02 0xe7 = 00000000 00000000 00000010 11100111 = 743
>
> Any help is appreciated.

C doesn't guarantee that UCHAR_MAX <= UINT_MAX nor does it guarantee
that UINT_MAX is >= 0xFFFFFFFF. But here is something that will work if
you can guarantee the values in the array are <= 0xFF:

#define lengthof(a) (sizeof(a)/sizeof(a[0]))

const unsigned char array[4] = { 0, 0, 0x02, 0xe7 };
unsigned long ui = 0;
unsigned long mult = 1;

/*
it looks like you wanted big endian conversion. For little
endian, just index the array in the regular order
*/
for (unsigned i = 0; i < 4; ++i) {
ui += mult * array[lengthof(array)-1-i];
mult <<= 8;
}

assert(ui == 743);

Otherwise replace unsigned long and unsigned char with the uint32_t and
uint8_t types in stdint.h which is probably what you intended anyway.

junky_fellow@yahoo.co.in
Guest
Posts: n/a

 06-17-2005

Me wrote:
> > I have an unsigned char array (size 4):
> >
> > unsigned char array[4];
> >
> > array[0] = 0x00;
> > array[1] = 0x00;
> > array[2] = 0x02;
> > array[3] = 0xe7;
> >
> > I would like to convert array[] to an integer.
> >
> > 0x00 0x00 0x02 0xe7 = 00000000 00000000 00000010 11100111 = 743
> >
> > Any help is appreciated.

>
> C doesn't guarantee that UCHAR_MAX <= UINT_MAX nor does it guarantee
> that UINT_MAX is >= 0xFFFFFFFF. But here is something that will work if
> you can guarantee the values in the array are <= 0xFF:
>
> #define lengthof(a) (sizeof(a)/sizeof(a[0]))
>
> const unsigned char array[4] = { 0, 0, 0x02, 0xe7 };
> unsigned long ui = 0;
> unsigned long mult = 1;
>
> /*
> it looks like you wanted big endian conversion. For little
> endian, just index the array in the regular order
> */

<snip>

You don't need to do that for little endian. Whether it is little
or big endian, array[0] will always be 0,
array[1] will always be 0,
array[2] will always be 0x02, and
array[3] will always be 0xe7.

Martin Ambuhl
Guest
Posts: n/a

 06-17-2005
No Such Luck wrote:
> Hi all:
>
> I have an unsigned char array (size 4):
>
> unsigned char array[4];
>
> array[0] = 0x00;
> array[1] = 0x00;
> array[2] = 0x02;
> array[3] = 0xe7;
>
> I would like to convert array[] to an integer.
>
> 0x00 0x00 0x02 0xe7 = 00000000 00000000 00000010 11100111 = 743

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

int main(void)
{
unsigned i;
unsigned char a[4] = { 0x00, 0x00, 0x02, 0xe7 };

i = a[3] | (a[2] | (a[1] | a[0] << CHAR_BIT) << CHAR_BIT) <<
CHAR_BIT;
printf("i built up with left shifts & ors: %u\n", i);
return 0;
}

i built up with left shifts & ors: 743

Peter Nilsson
Guest
Posts: n/a

 06-17-2005
Me wrote:
> C doesn't guarantee that UCHAR_MAX <= UINT_MAX...

Yes, it does. [cf 6.2.5p8]

--
Peter

Me
Guest
Posts: n/a

 06-17-2005
> > C doesn't guarantee that UCHAR_MAX <= UINT_MAX...
>
> Yes, it does. [cf 6.2.5p8]

Yeah, that was really stupid of me. I meant to say that C doesn't
guarantee that pow(UCHAR_MAX+1,4)-1 <= UINT_MAX.

Richard Bos
Guest
Posts: n/a

 06-17-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

[ If you _must_ use Google Broken Beta to post to Usenet, could you
on how to do so abound on Usenet, including in someone's sig here. ]

> first u need to understand the relationship between BCD representation.

BCD is irrelevant to the OP's problem; he's trying to concatenate bytes
into a (normal binary) integer, nothing more complicated.

> like 3 is represented as 0111 and not 111.

Where'd you get that? Even in BCD, 3 is represented by 0011, and 0111 is
7. That alone should've tipped you off that BCD is not the issue.

BTW, if you want to be taken seriously as a professional, learn to write
without script-kiddyisms such as "u" for you.

Richard

pete
Guest
Posts: n/a

 06-17-2005
Martin Ambuhl wrote:
>
> No Such Luck wrote:
> > Hi all:
> >
> > I have an unsigned char array (size 4):
> >
> > unsigned char array[4];
> >
> > array[0] = 0x00;
> > array[1] = 0x00;
> > array[2] = 0x02;
> > array[3] = 0xe7;
> >
> > I would like to convert array[] to an integer.
> >
> > 0x00 0x00 0x02 0xe7 = 00000000 00000000 00000010 11100111 = 743

>
> #include <stdio.h>
> #include <limits.h>
>
> int main(void)
> {
> unsigned i;
> unsigned char a[4] = { 0x00, 0x00, 0x02, 0xe7 };
>
> i = a[3] | (a[2] | (a[1] | a[0] << CHAR_BIT) << CHAR_BIT) <<
> CHAR_BIT;
> printf("i built up with left shifts & ors: %u\n", i);
> return 0;
> }
>
> i built up with left shifts & ors: 743

If you're trying to make 743,
then it should be 8 instead of CHAR_BIT.

--
pete

Lawrence Kirby
Guest
Posts: n/a

 06-17-2005
On Thu, 16 Jun 2005 21:30:13 -0700, Me wrote:

>> I have an unsigned char array (size 4):
>>
>> unsigned char array[4];
>>
>> array[0] = 0x00;
>> array[1] = 0x00;
>> array[2] = 0x02;
>> array[3] = 0xe7;
>>
>> I would like to convert array[] to an integer.
>>
>> 0x00 0x00 0x02 0xe7 = 00000000 00000000 00000010 11100111 = 743
>>
>> Any help is appreciated.

>
> C doesn't guarantee that UCHAR_MAX <= UINT_MAX nor does it guarantee

It does but that has been covered in other replies.

> that UINT_MAX is >= 0xFFFFFFFF. But here is something that will work if
> you can guarantee the values in the array are <= 0xFF:

Right.

> #define lengthof(a) (sizeof(a)/sizeof(a[0]))
>
> const unsigned char array[4] = { 0, 0, 0x02, 0xe7 };
> unsigned long ui = 0;
> unsigned long mult = 1;
>
> /*
> it looks like you wanted big endian conversion. For little
> endian, just index the array in the regular order
> */
> for (unsigned i = 0; i < 4; ++i) {
> ui += mult * array[lengthof(array)-1-i];
> mult <<= 8;
> }

Or perhaps

for (unsigned i = 0; i < lengthof(array); i++)
ui = (ui << | array[i];

> assert(ui == 743);
>
> Otherwise replace unsigned long and unsigned char with the uint32_t and
> uint8_t types in stdint.h which is probably what you intended anyway.

You're much better off using unsigned long and unsigned char. They do the
job portably whereas uint32_t and uint8_t are non-=portable because an
implementation is only required to support them if it has a type with the
appropriate representation available. And of course they aren't supported
in C90 at all.

Lawrence