Note that there are some explanatory texts on larger screens.

plurals
  1. POReversing byte order in .NET
    text
    copied!<p>In the code below, why do X and Y take on different values than what I would think intuitively?</p> <p>If the bytes 0-7 are written to the buffer, shouldn't the resulting long have bytes in the same order? It's like it's reading the long values in reverse order.</p> <pre><code>x 0x0706050403020100 long y 0x0706050403020100 long z 0x0001020304050607 long MemoryStream ms = new MemoryStream(); byte[] buffer = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }; ms.Write(buffer, 0, buffer.Length); ms.Flush(); ms.Position = 0; BinaryReader reader = new BinaryReader(ms); long x = reader.ReadInt64(); long y = BitConverter.ToInt64(buffer, 0); long z = BitConverter.ToInt64(buffer.Reverse&lt;byte&gt;().ToArray&lt;byte&gt;(), 0); byte[] xbytes = BitConverter.GetBytes(x); byte[] ybytes = BitConverter.GetBytes(y); byte[] zbytes = BitConverter.GetBytes(z); </code></pre> <p>(I don't know what to tag this question, beyond just .NET.)</p> <hr> <pre><code>BitConverter.IsLittleEndian </code></pre> <p>is false. If my computer is big endian, why does this happen?</p> <ul> <li>This is a Windows 7 64-bit machine</li> <li>Intel Core2 Quad Q9400 2.66&nbsp;GHz LGA 775 95W Quad-Core Processor Model BX80580Q9400</li> <li>SUPERMICRO MBD-C2SBX+-O LGA 775 Intel X48 ATX Intel Motherboard</li> </ul> <p>The results of this code (in response to Jason's comment):</p> <pre><code>byte[] buffer = new byte[] { 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }; long y = BitConverter.ToInt64(buffer, 1); Console.WriteLine(BitConverter.IsLittleEndian); Console.WriteLine(y); </code></pre> <p>Result:</p> <pre><code>False 506097522914230528 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload