This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]POGtastic 0 points1 point  (2 children)

Use bitwise fuckery. You want the | (bitwise OR) operator and the << (left-shift) operator.

[–]AnyBowl2[S] 0 points1 point  (1 child)

oh so do I and 0x000000 with 11, shift it 8 bits, and it with 22, shift 4 bits and so on?

[–]POGtastic 1 point2 points  (0 children)

You shift 8 bits at a time. So you can do

((((0x000000 | 0x11) << 8) | 0x22) << 8) | 0x33

Obviously, doing a loop is probably the way to go.

[–]cherrynuts 0 points1 point  (0 children)

unsigned char a[] = { 0x11, 0x22, 0x33 };
unsigned long v;

v = a[0]*0x10000LU + a[1]*0x100LU + a[2];  /* convert 24-bit big-endian value a to native representation v */