Converting Byte Array to Unsigned long Array

Discussion in 'Programmer's Corner' started by eng.me, Jun 15, 2013.

  1. eng.me

    Thread Starter New Member

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

    Code ( (Unknown Language)):
    1. uint8_t data [8] = { 0x67 , 0x45 , 0x32 ,  0x01 ,  0xEF ,  0xCD ,  0xAB ,  0x89 };        
    I need to convert it to :
    Code ( (Unknown Language)):
    1.  unsigned long data [8] = { 0x01234567 , 0x89ABCDEF };        
     
  2. MrChips

    Moderator

    Oct 2, 2009
    12,449
    3,363
    Use a union:

    Code ( (Unknown Language)):
    1.  
    2. typedef union
    3. {
    4.   uint8_t data8[8];
    5.   unsigned long data32[2];
    6. }mydata;
    7.  
     
  3. WBahn

    Moderator

    Mar 31, 2012
    17,757
    4,800
    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.
     
Loading...