/// <summary> /// Convert a Base62 string to byte array /// </summary> /// <param name="base62">Base62 string</param> /// <returns>Byte array</returns> public static byte[] FromBase62(string base62) { // Character count int count = 0; // Set up the BitStream BitStream stream = new BitStream(base62.Length * 6 / 8); foreach (char c in base62) { // Look up coding table int index = Base62CodingSpace.IndexOf(c); // If end is reached if (count == base62.Length - 1) { // Check if the ending is good int mod = (int)(stream.Position % 8); if (mod == 0) throw new InvalidDataException("an extra character was found"); if ((index >> (8 - mod)) > 0) throw new InvalidDataException("invalid ending character was found"); stream.Write(new byte[] { (byte)(index << mod) }, 0, 8 - mod); } else { // If 60 or 61 then only write 5 bits to the stream, otherwise 6 bits. if (index == 60) { stream.Write(new byte[] { 0xf0 }, 0, 5); } else if (index == 61) { stream.Write(new byte[] { 0xf8 }, 0, 5); } else { stream.Write(new byte[] { (byte)index }, 2, 6); } } count++; } // Dump out the bytes byte[] result = new byte[stream.Position / 8]; stream.Seek(0, SeekOrigin.Begin); stream.Read(result, 0, result.Length * 8); return result; }