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.

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));

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).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.