title: Converting float/double/decimal to (u)int, (u)long in C# without memory allocation
body: <p>I am working on the c# networking solution for my game, and I have a problem with serializing <code>float</code>s, <code>double</code>s, and <code>decimal</code>s.</p>
  <p>My first attempt - <code>BitConverter</code> class. But each time I use that class, a new byte[] array is created, and this may be overkill for my net solution (for example - player position synchronization).</p>
  <p>To summarize, my expectation is:</p>
  <ul>
  <li>Possibility to convert <code>float</code>s, <code>double</code>s, and <code>decimal</code>s into (u)int, (u)long without memory allocation.</li>
  <li>Possibility to read (u)int's and (u)longs from server &amp; convert the, back to the original type (still without allocating additional memory).</li>
  </ul>
  <p><strong>Edit 1</strong></p>
  <p>This is method I use in my NetworkWriter method to store bytes to send:</p>
  <pre class="lang-cs prettyprint-override"><code>public void WriteByte(byte value)
          {
              Buffer[_position++] = value;
          }
  </code></pre>
  <p>And those are my extensions for NetworkWriter:</p>
  <pre><code>public static void WriteUInt(this NetworkWriter writer, uint value)
          {
              writer.WriteByte((byte) value);
              writer.WriteByte((byte) (value &gt;&gt; 8));
              writer.WriteByte((byte) (value &gt;&gt; 16));
              writer.WriteByte((byte) (value &gt;&gt; 24));
          }
  </code></pre>
  <p>That's I want to cast 4-byte float to 4-byte (u)int, so I can pass the result int to the Write(U)Int serializer.</p>
  <p>I haven't done the NetworkReader yet, so I cannot show you the exact code, but it's gonna revert the Write method.</p>
  <p>I expect a lot of float/double operations, because:</p>
  <ul>
  <li>Vector3 (class that holds player position) is a pack of 3 floats,</li>
  <li>Latency measuring will send/receive double time</li>
  <li>Quaternion (class that holds player rotation) is also pack of 4 floats.</li>
  </ul>
  <p>That's why I wanna reduce memory allocation for float/double/decimal as much as possible.</p>
  <p><strong>Edit 2</strong></p>
  <p>Important note: I know when to expect float, double, etc, let me quickly pseudocode how will reader look like:</p>
  <pre><code>static float ReadFloat():
  - Get 4 int bytes from buffer
  - Construct float from those
  </code></pre>