Base 36 Decode/Encode - CSharp System

CSharp examples for System:String Base

Description

Base 36 Decode/Encode

Demo Code


using System;/*  w  w w .j av a 2  s  .c  o m*/
using System.Text;


    public static class Base36
    {
        private const string Clist = "0123456789abcdefghijklmnopqrstuvwxyz";
        private static readonly char[] Clistarr = Clist.ToCharArray();

        public static long Decode(string inputString)
        {
            long result = 0;
            var pow = 0;
            for (var i = inputString.Length - 1; i >= 0; i--)
            {
                var c = inputString[i];
                var pos = Clist.IndexOf(c);
                if (pos > -1)
                    result += pos * (long)Math.Pow(Clist.Length, pow);
                else
                    return -1;
                pow++;
            }
            return result;
        }

        public static string Encode(ulong inputNumber)
        {
            var sb = new StringBuilder();
            do
            {
                sb.Append(Clistarr[inputNumber % (ulong)Clist.Length]);
                inputNumber /= (ulong)Clist.Length;
            } while (inputNumber != 0);
            return Reverse(sb.ToString());
        }

        public static string Reverse(string s)
        {
            if (s == null)
                throw new ArgumentException("s is null");

            var charArray = s.ToCharArray();
            Array.Reverse(charArray);
            return new string(charArray);
        }
    }

Related Tutorials