Combine hexadecimal numbers in C

I am trying to combine 4 hexadecimal numbers and cannot do this.

Example:

int a = 0x01; int b = 0x00; int c = 0x20; int d = 0xF1; //Result should be 0x010020F1 

The results I get with sprintf and bitwise operations always cut off zeros, giving me answers like 1020F1, which are very different from what I want. Does anyone have a better method?

+5
source share
1 answer

Suppose unsigned int a,b,c,d;

 unsigned int result = (a<<24) | (b<<16)| (c<<8) | d; 

But it mainly depends on the implementation, since the C ++ standard defines only the minimum sizes of integers.

So for uint32_t a, b, c, d :

 uint32_t result = (a<<24) | (b<<16)| (c<<8) | d; 
+18
source

All Articles