title: Most efficient way to convert BCD to binary
body: <p>I have the code below to convert a 32 bit BCD value (supplied in two uint halves) to a uint binary value.</p>
  
  <p>The values supplied can be up to 0x9999, to form a maximum value of 0x99999999. </p>
  
  <p>Is there a better (ie. quicker) way to achieve this? </p>
  
  <pre><code>    /// &lt;summary&gt;
      /// Convert two PLC words in BCD format (forming 8 digit number) into single binary integer.
      /// e.g. If Lower = 0x5678 and Upper = 0x1234, then Return is 12345678 decimal, or 0xbc614e.
      /// &lt;/summary&gt;
      /// &lt;param name="lower"&gt;Least significant 16 bits.&lt;/param&gt;
      /// &lt;param name="upper"&gt;Most significant 16 bits.&lt;/param&gt;
      /// &lt;returns&gt;32 bit unsigned integer.&lt;/returns&gt;
      /// &lt;remarks&gt;If the parameters supplied are invalid, returns zero.&lt;/remarks&gt;
      private static uint BCD2ToBin(uint lower, uint upper)
      {
          uint binVal = 0;
  
          if ((lower | upper) != 0)
          {
              int shift = 0;
              uint multiplier = 1;
              uint bcdVal = (upper &lt;&lt; 16) | lower;
  
              for (int i = 0; i &lt; 8; i++)
              {
                  uint digit = (bcdVal &gt;&gt; shift) &amp; 0xf;
  
                  if (digit &gt; 9)
                  {
                      binVal = 0;
                      break;
                  }
                  else
                  {
                      binVal += digit * multiplier;
                      shift += 4;
                      multiplier *= 10;
                  }
              }
          }
  
          return binVal;
      }
  </code></pre>