title: How to convert a float to BCD
body: <p>How can I convert a positive float (ieee 754, single precision 32 bit) to BCD in C#?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>If I'm not wrong, what I need is called packed BCD. I'm already doing int to BCD conversions. In the way I'm doing, the number 1234 would result in the following byte array:</p>
  
  <pre><code>{0x00, 0x00, 0x12, 0x34}
  </code></pre>
  
  <p>The method I use (which probably isn't the best way of doing it) is:</p>
  
  <pre><code>public byte[] ToBCD(long num) {
      long bcd = 0;
      int i = 0;
      while (num &gt; 9) {
          bcd |= ((num % 10) &lt;&lt; i);
          num /= 10;
          i += 4;
      }
      byte[] bytes = BitConverter.GetBytes(bcd | (num &lt;&lt; i));
      Array.Reverse(bytes);
      return bytes;
  }
  </code></pre>