Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I combine 4 bytes into a 32 bit unsigned integer?

I'm trying to convert 4 bytes into a 32 bit unsigned integer.

I thought maybe something like:

UInt32 combined = (UInt32)((map[i] << 32) | (map[i+1] << 24) | (map[i+2] << 16) | (map[i+3] << 8));

But this doesn't seem to be working. What am I missing?

like image 824
Pocki Avatar asked Jun 20 '11 02:06

Pocki


1 Answers

Your shifts are all off by 8. Shift by 24, 16, 8, and 0.

like image 144
Ignacio Vazquez-Abrams Avatar answered Sep 23 '22 13:09

Ignacio Vazquez-Abrams