title: Base-N encoding of a byte array
body: <p>A couple of days ago I came across <a href="https://codereview.stackexchange.com/questions/14084/base-36-encoding-of-a-byte-array/">this CodeReview</a> for Base-36 encoding a byte array. However, the answers that followed didn't touch on decoding back into a byte array, or possibly reusing the answer to perform encodings of different bases (radix).</p>
  
  <p>The answer for the linked question uses BigInteger. So as far as implementation goes, the base and its digits could be parametrized.</p>
  
  <p>The problem with BigInteger though, is that we're treating our input as an assumed integer. However, our input, a byte array, is just an opaque series of values.</p>
  
  <ul>
  <li>If the byte array ends in a series of zero bytes, eg {0xFF,0x7F,0x00,0x00}, those bytes will be lost when using the algorithm in the answer (would only encode {0xFF,0x7F}. </li>
  <li>If the last non-zero byte has the sign bit set then the proceeding zero byte is consumed as it's treated as the BigInt's sign delimiter. So {0xFF,0xFF,0x00,0x00} would encode only as {0xFF,0xFF,0x00}.</li>
  </ul>
  
  <p><em>How could a .NET programmer use BigInteger to create a reasonably efficient and radix-agnostic encoder, with decoding support, plus the ability to handle endian-ness, and with the ability to 'work around' the ending zero bytes being lost?</em></p>