title: Base64-like string conversion, to smaller character set
body: <p>I have a series of encrypted strings that I use as ids in a C#/ASP.Net web application. I need to use these in the <code>id</code> attribute, however, the string contain illegal characters (valid characters are only ´[A-Za-z0-9-_:.]`). I need a <strong>two-way</strong> conversion that maps my encrypted strings to this small set. Something like Base64, but smaller.</p>
  
  <p>What are my alternatives? Are there standard algorithms for this, or is it odd enough I have to invent it myself? </p>
  
  <p><strong>Solution:</strong>
  In case someone needs this, here's what I ended up doing. Replace non-valid characters, and remove the padding = char. Then undo this to revert.</p>
  
  <pre><code>    private static string MakeReferenceJavascriptCompatible(string reference)
      {
          return reference.Replace("+", "_")
                          .Replace("/", "-")
                          .Replace("=", "");
      }
      private static string UndoMakeReferenceJavascriptCompatible(string reference)
      {
          int padding = 4 - (reference.Length % 4);
          return reference.Replace("-", "/")
                          .Replace("_", "+")
                          .PadRight(reference.Length + padding, '=');
      }
  </code></pre>