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!
Creating <b>int</b> from BYTE Array
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...
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
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
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:
- 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 itinline 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);}
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
quote:Original post by CProgrammerquote:
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
Thanks a bunch Odd the Hermit!
-CProgrammer
[edited by - CProgrammer on August 13, 2003 4:42:26 PM]
-CProgrammer
[edited by - CProgrammer on August 13, 2003 4:42:26 PM]
quote:Original post by Dak Lozar" Loeser
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
Thanks a lot this helps a bunch!
Hey, don''t forget to visit my page... by clicking here!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement