Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is because of the little <a href="http://en.wikipedia.org/wiki/Endianness" rel="noreferrer">endianness</a> of your machine. See <a href="http://msdn.microsoft.com/en-us/library/system.bitconverter.islittleendian.aspx" rel="noreferrer"><code>BitConverter.IsLittleEndian</code></a> property to check this.</p> <p>Basically, numbers are stored in reverse byte order, compared to how you would write them down. We write the most significant number on the left, but the (little endian) PC stores the <strong>least</strong> significant byte on the left. Thus, the result you're getting is really <code>0xBEBAFECA</code> (3199925962 decimal) and not what you expected.</p> <p>You can convert using bit shifting operations:</p> <pre><code>uint value = (f2[0] &lt;&lt; 24) | (f2[1] &lt;&lt; 16) | (f2[2] &lt;&lt; 8) | f2[3]; </code></pre> <p>There are many more ways to convert, including <a href="http://msdn.microsoft.com/en-us/library/x1886wea.aspx" rel="noreferrer"><code>IPAddress.NetworkToHostOrder</code></a> as I4V pointed out, <code>f2.Reverse()</code>, etc.</p> <p>For your specific code, I believe this would be most practical:</p> <pre><code>uint num = (uint)IPAddress.NetworkToHostOrder(in_.ReadInt32()); </code></pre> <p>This may result in an arithmetic underflow however, so it may cause problems with a <a href="http://msdn.microsoft.com/en-us/library/khy08726%28v=vs.110%29.aspx" rel="noreferrer"><code>/checked</code> compiler option or <code>checked</code> keyword</a> (neither are very common).</p> <p>If you want to deal with these situations and get even cleaner code, wrap it in an extension method:</p> <pre><code>public static uint ReadUInt32NetworkOrder(this BinaryReader reader) { unchecked { return (uint)IPAddress.NetworkToHostOrder(reader.ReadInt32()); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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