C#实现10进制转32进制和64进制,排除0、l(L)和o

class Code32Demo
    {
        static void Main(string[] args)
        {
            Console.WriteLine(IntToi32(121221021));
            Console.WriteLine(IntToi32(803131021));
            Console.WriteLine(IntToi32(30113451));
            Console.WriteLine(IntToi64(121221021));
            Console.WriteLine(IntToi64(803131021));
            Console.WriteLine(IntToi64(30113451));

            Console.ReadLine();
        }

        public static string IntToi32(long xx)
        {
            string a = "";
            while (xx >= 1)
            {
                int index = Convert.ToInt16(xx - (xx / 32) * 32);
                a = Base64Code[index] + a;
                xx = xx / 32;
            }
            return a;
        }

        public static long i32ToInt(string xx)
        {
            long a = 0;
            int power = xx.Length - 1;

            for (int i = 0; i <= power; i++)
            {
                a += _Base64Code[xx[power - i].ToString()] * Convert.ToInt64(Math.Pow(32, i));
            }

            return a;
        }


        public static string IntToi64(long xx)
        {
            string a = "";
            while (xx >= 1)
            {
                int index = Convert.ToInt16(xx - (xx / 64) * 64);
                a = Base64Code[index] + a;
                xx = xx / 64;
            }
            return a;
        }

        public static long i64ToInt(string xx)
        {
            long a = 0;
            int power = xx.Length - 1;

            for (int i = 0; i <= power; i++)
            {
                a += _Base64Code[xx[power - i].ToString()] * Convert.ToInt64(Math.Pow(64, i));
            }

            return a;
        }

        public static Dictionary<int, string> Base64Code = new Dictionary<int, string>() {
            {   0  ,"z"}, {   1  ,"1"}, {   2  ,"2"}, {   3  ,"3"}, {   4  ,"4"}, {   5  ,"5"}, {   6  ,"6"}, {   7  ,"7"}, {   8  ,"8"}, {   9  ,"9"},
            {   10  ,"a"}, {   11  ,"b"}, {   12  ,"c"}, {   13  ,"d"}, {   14  ,"e"}, {   15  ,"f"}, {   16  ,"g"}, {   17  ,"h"}, {   18  ,"i"}, {   19  ,"j"},
            {   20  ,"k"}, {   21  ,"x"}, {   22  ,"m"}, {   23  ,"n"}, {   24  ,"y"}, {   25  ,"p"}, {   26  ,"q"}, {   27  ,"r"}, {   28  ,"s"}, {   29  ,"t"},
            {   30  ,"u"}, {   31  ,"v"}, {   32  ,"w"}, {   33  ,"x"}, {   34  ,"y"}, {   35  ,"z"}, {   36  ,"A"}, {   37  ,"B"}, {   38  ,"C"}, {   39  ,"D"},
            {   40  ,"E"}, {   41  ,"F"}, {   42  ,"G"}, {   43  ,"H"}, {   44  ,"I"}, {   45  ,"J"}, {   46  ,"K"}, {   47  ,"L"}, {   48  ,"M"}, {   49  ,"N"},
            {   50  ,"O"}, {   51  ,"P"}, {   52  ,"Q"}, {   53  ,"R"}, {   54  ,"S"}, {   55  ,"T"}, {   56  ,"U"}, {   57  ,"V"}, {   58  ,"W"}, {   59  ,"X"},
            {   60  ,"Y"}, {   61  ,"Z"}, {   62  ,"-"}, {   63  ,"_"},
        };

        public static Dictionary<string, int> _Base64Code
        {
            get
            {
                return Enumerable.Range(0, Base64Code.Count()).ToDictionary(i => Base64Code[i], i => i);
            }
        }
    }

 

转载于:https://my.oschina.net/lizongyue/blog/1634139

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值