0

i have a char array of 4 elements :

00000000 00000000 00000010 00000111

how can i assign these bytes to 4 byte integer (this means 519), could you please give a modular code sample?

thanx.

3
Contributors
2
Replies
7
Views
9 Years
Discussion Span
Last Post by ashishtrivedi
0

If that is the binary representation of the integer, then just typecast it or memcpy() it.

char array[some_number];

int n = *(int *)array;

or
memcpy(&n, array, sizeof(int));
0

If that is the binary representation of the integer, then just typecast it or memcpy() it.

char array[some_number];

int n = *(int *)array;

or
memcpy(&n, array, sizeof(int));

Hi Guys,

I tried the code suggested by Ancient Dragon. But it looks like its dependent on bit pattern followed by your processor (little andien - big Andien).

So I got the bits transferred to
0x07020000 instead of 0x00000207...

You can try may be the following code snippet. It should work independent of any processor.

unsigned char tempArr[4]={0x00, 0x00, 0x02, 0x07}; //00000000, 00000000, 00000010, 00000111};
    int i=0, arrToInt=0;
    for(i=0;i<4;i++)
        arrToInt =(arrToInt<<8) | tempArr[i];

The above code converts the array into 4 byte integer with array's 0th element as MSB (Most Sygnificant Byte) and last element as LSB(Least Sygnificant Byte).

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.