Just wondering if someone could explain why the two following lines of code return "different" results? What causes the reversed values? Is this something to do with endianness?
int.MaxValue.ToString("X") //Result: 7FFFFFFF
BitConverter.ToString(BitConverter.GetBytes(int.MaxValue)) //Result: FF-FF-FF-7F
int.MaxValue.ToString("X")
outputs7FFFFFFF
, that is, the number2147483647
as a whole.On the other hand,
BitConverter.GetBytes
returns an array of bytes representing2147483647
in memory. On your machine, this number is stored in little-endian (highest byte last). AndBitConverter.ToString
operates separately on each byte, therefore not reordering output to give the same as above, thus preserving the memory order.However the two values are the same :
7F-FF-FF-FF
forint.MaxValue
, in big-endian, andFF-FF-FF-7F
forBitConverter
, in little-endian. Same number.I would guess because
GetBytes
returns an array of bytes whichBitConverter.ToString
formatted - in my opinion - rather nicelyAnd also keep in mind that the bitwise represantattion may be different from the value! This depends where the most signigicant byte sits!
hth