Archived

This topic is now archived and is closed to further replies.

Creating <b>int</b> from BYTE Array

This topic is 5224 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have a problem. I''m trying to fill a int data type using a BYTE array of 4 characters. Here''s what I''m tying to do... int AnInt = 0; BYTE IntBuff[4]; // Let''s say... // IntBuff[0] = 1 // IntBuff[1] = 2 // IntBuff[2] = 3 // IntBuff[3] = 4 for (int x=0;x<4;x++) AnInt ??? IntBuff[x]; See where the question marks are. That''s where I''m stuck. What am I supposed to do there? Should I do an "&=" or "|=" or something totally different? Hey, don''t forget to visit my page... by clicking here!

Share this post


Link to post
Share on other sites
You should be able to remove the loop and just do the following:

int AnInt = 0;
BYTE IntBuff[4];

// Let''s say...

IntBuff[0] = 1;
IntBuff[1] = 2;
IntBuff[2] = 3;
IntBuff[3] = 4;

// And then later
AnInt = *(int*)IntBuff;

Dave "Dak Lozar" Loeser

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The cast works if:

- The byte array is properly aligned for the architecture (i e always on x86, not always on anything else).

- The byte array uses the same byte order as the native machine order.

Here are two functions:



COMPILE_ASSERT(sizeof(int)==4) // remove this if you don''t have it

inline unsigned int readUintFromLittleEndianByteArray( unsigned char const * data ) {
return data[0] | (data[1]<<8) | (data[2]<<16) | (data[3]<<24);
}

inline unsigned int readUintFromBigEndianByteArray( unsigned char const * data ) {
return data[3] | (data[2]<<8) | (data[1]<<16) | (data[0]<<24);
}

Share this post


Link to post
Share on other sites
quote:
Original post by CProgrammer
quote:

return data[3] | (data[2]<<8) | (data[1]<<16) | (data[0]<<24);


I didnt know you could do that.
Could you explain whats happening here in detail?
Thanks
-CProgrammer


Bit math. The | is bitwise or; think of it like the + operator. The << is a left shift; the number after it is how many places to shift by. So, 1<<2 is:
1 == 00000001
1<<2 == 00000100 == 4. Basically, you shift each byte''s value over appropriately (8, 16, or 24 bits); the sum is the integer you want.

The difference between the two functions is whether you''re working on a big-endian system or a small-endian system. For that, I recommend google.

-Odd the Hermit

Share this post


Link to post
Share on other sites
quote:
Original post by Dak Lozar
You should be able to remove the loop and just do the following:

int AnInt = 0;
BYTE IntBuff[4];

// Let''s say...

IntBuff[0] = 1;
IntBuff[1] = 2;
IntBuff[2] = 3;
IntBuff[3] = 4;

// And then later
AnInt = *(int*)IntBuff;

Dave "Dak Lozar" Loeser


Thanks a lot this helps a bunch!


Hey, don''t forget to visit my page... by clicking here!

Share this post


Link to post
Share on other sites