Converting Byte Array to Unsigned long Array

Thread Starter

eng.me

Joined Dec 10, 2012
27
Can some one help me to convert the following byte array to unsigned long array

Rich (BB code):
uint8_t data [8] = { 0x67 , 0x45 , 0x32 ,  0x01 ,  0xEF ,  0xCD ,  0xAB ,  0x89 };
I need to convert it to :
Rich (BB code):
 unsigned long data [8] = { 0x01234567 , 0x89ABCDEF };
 

MrChips

Joined Oct 2, 2009
19,270
Use a union:

Rich (BB code):
typedef union
{
  uint8_t data8[8];
  unsigned long data32[2];
}mydata;
 

WBahn

Joined Mar 31, 2012
24,684
How that behaves is going to depend on the endiness of the machine -- this is geared to a little-endian machine.

Also, I don't know that the size of a long is rigidly fixed at 32 bits versus just having a minimum width of 32 bits. It would be better to use something like uint32_t, instead.

Finally, this won't work for the values given by the OP, though I suspect the cause is a simple typo.
 
Top