c# – How to form 32 bit integer by using four custom bytes?
You can construct such int
explicitly with a help of bit operations:
int result = myByte_4 << 24 |
myByte_3 << 16 |
myByte_2 << 8 |
myByte_1;
please, note that we have an integer overflow and result
is a negative number:
Console.Write($"{result} (0x{result:X})");
Outcome;
-573785174 (0xDDCCBBAA)
BitConvertor
is an alternative, which is IMHO too wordy:
int result = BitConverter.ToInt32(BitConverter.IsLittleEndian
? new byte[] { myByte_1, myByte_2, myByte_3, myByte_4 }
: new byte[] { myByte_4, myByte_3, myByte_2, myByte_1 });
Read more here: Source link