Convert a string into BASE62

fubo picture fubo · Jul 19, 2013 · Viewed 15.5k times · Source

I'm looking for the c# code to convert a string into BASE62, like this:

http://www.molengo.com/base62/title/base62-encoder-decoder

I need those encode and decode-methods for URL-Encoding.

Answer

CSmith picture CSmith · Jul 19, 2013

Background on BINARY to TEXT Encoding schemes:

https://en.wikipedia.org/wiki/Base62

https://en.wikipedia.org/wiki/Base64

Good explanation of the BASE62 encoding scheme:

https://www.codeproject.com/Articles/1076295/Base-Encode

Try the C# libraries available here which adds some extension methods to allow you to convert a byte array to and from BASE62 (binary-to-text encoding schemes).

Plenty of base62 libraries on github, have a look:

If your source data is contained in a "string" then you would first need to convert your "string" to a suitable byte array.

But be careful, to use the correct string to byte conversion call....as you may want the bytes to be the ASCII characters, or the Unicode byte stream etc i.e. Encoding.GetBytes(text) or System.Text.ASCIIEncoding.ASCII.GetBytes(text);, etc

byte[] bytestoencode = ..... 

string encodedasBASE62 = bytestoencode.ToBase62();

.....

byte[] bytesdecoded = encodedasBASE62.FromBase62();