I try to convert an Ip Address to a long value :
byte[] Ip = new byte[4] { 192, 168, 1, 0 };
UInt32 Ret1 = (((UInt32)Ip[0]) << 24) |
(((UInt32)Ip[1]) << 16) |
(((UInt32)Ip[2]) << 8) |
(((UInt32)Ip[3]));
UInt32 Ret2 = BitConverter.ToUInt32(Ip, 0);
Ret1 returns 3232235776
(the correct value)
Ret2 returns 108736
(?)
Why this difference ?
Indeed, endianness is your issue here. While not difficult to work around, it can be annoying at times on Intel-based systems due to them being in Little Endian whereas Network Order is Big Endian. In short, your bytes are in the reverse order.
Here is a little convenience method you may use to solve this issue (and even on various platforms):