• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

用C#简单实现的36进制转换代码

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
private const string initValue = "A0000001";

        private static string cs = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

        public static string AddOne(string str)
        {
            if (str == string.Empty)
            {
                return initValue;
            }
            Int64 num = StringToNumber(str);
            return NumberToString(++num);
        }

        public static Int64 StringToNumber(string str)
        {
            int leg = str.Length;
            double num = 0;
            if (leg != 0)
            {
                for (int i = 0; i < leg; i++)
                {
                    if (str[i] != '0')
                    {
                        num += cs.IndexOf(str[i]) * Math.Pow(36, leg - 1 - i);
                    }
                }
            }
            return Convert.ToInt64(num);
        }

        public static string NumberToString(Int64 num)
        {
            string str = string.Empty;
            while (num >= 36)
            {
                str = cs[(int)(num % 36)] + str;
                num = num / 36;
            }
            return cs[(int)num] + str;
        }

 2019-8-30改进的版本

class Program {
        static void Main(string[] args) {
            string curr = "ZZZZZZ";
            Console.WriteLine("原始值:" + curr);
            Int64 num = Scale.ToInt64(curr);
            Console.WriteLine("转成Int64:" + num);
            Console.WriteLine("根据Int64转成进制:" + Scale.ToCurr(num));
            Console.ReadLine();
        }
    }
    public static class Scale {
        /// <summary>
        /// 进制符号字符串
        /// </summary>
        private static string scString = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ";
        /// <summary>
        /// 字符集,可以根据编号索引拿到字符
        /// </summary>
        private static char[] scArray = scString.ToCharArray();
        /// <summary>
        /// 字符字典,可以根据字符拿到编号索引
        /// </summary>
        private static Dictionary<char, int> scDic = ToCharDic();
        /// <summary>
        /// 根据字符串反馈进制数
        /// </summary>
        public static int Len { get { return scString.Length; } }

        /// <summary>
        /// 将字符串处理成字符字典
        /// </summary> 
        private static Dictionary<char, int> ToCharDic() {
            Dictionary<char, int> dic = new Dictionary<char, int>();
            for (int i = 0; i < scArray.Length; i++) {
                dic.Add(scArray[i], i);
            }
            return dic;
        }
        /// <summary>
        /// 根据传入的字符符号定义进制,字符符号不能重复,模拟十进制字符串为:0123456789
        /// </summary> 
        public static void SetScale(string scaleString) {
            scString = scaleString;
            scArray = scString.ToCharArray();
            scDic = ToCharDic();
        }
        /// <summary>
        /// 将Int64转成当前进制字符串
        /// </summary> 
        public static string ToCurr(long num) {
            string curr = "";
            while (num >= Len) {
                curr = scArray[num % Len] + curr;
                num = num / Len;
            }
            curr = scArray[num] + curr;
            return curr;
        }
        /// <summary>
        /// 将当前进制字符串转成Int64
        /// </summary> 
        public static long ToInt64(string curr) {
            double num = 0;
            for (int i = 0; i < curr.Length; i++) {
                num += scDic[curr[i]] * Math.Pow(Len, curr.Length - 1 - i);
            }
            return Convert.ToInt64(num);
        }
    }

  


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#制作爬虫详细教程发布时间:2022-07-14
下一篇:
C#画图(1)发布时间:2022-07-14
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap