Converting unsigned char[] to long?

How can you access the individual bytes making up a variable of type long? What I need to do is move a 24-bit (3 bytes) unsigned char array into a long variable. An example:

unsigned char a[3];
unsigned long b;

a[0] = 0x02;
a[1] = 0xFF;
a[2] = 0xFF;

?????
?????


TIA!
James

Parents
  • You could use a union, cast a pointer, or do it with shifts.

    Shifting is portable but may be slow;
    The others are non-portable, but likely to be fast.

    You will also need to read the section in the Manual about C51's internal data representation - byte ordering, etc

    You should also do a 'Search, as it's been discussed many times before!
    (hence just a brief answer here)

Reply
  • You could use a union, cast a pointer, or do it with shifts.

    Shifting is portable but may be slow;
    The others are non-portable, but likely to be fast.

    You will also need to read the section in the Manual about C51's internal data representation - byte ordering, etc

    You should also do a 'Search, as it's been discussed many times before!
    (hence just a brief answer here)

Children
More questions in this forum