获取下一个三十六进制数

前两天帮公司女同事写了个三十六进制的算法,也比较简单,为了扩大我博客的文章数量,特地拿来充数。
同事需要根据一个三位数得到下一个三位数,且三位数是三十六进制的。也是就是'018'、'019'、'01A'、'01B'、'01C'这样,当然有最大的三位数'ZZZ',既从'000'到'ZZZ'共42840个数。于是就有了以下的代码。
 
         static void GetNextNo(ref string value)
        {
            value = value.ToUpper();
            if (value == "ZZZ")
            {
                throw new Exception("已经是最大编号,请联系管理员!");
            }
            temp = value.ToArray();
            int i = temp.Length - 1;
            push(i, temp[i]);
            value = new String(temp);
        }
 
        static void push(int i, char last)
        {
            if (last >= 65 && last <= 90)
            {
                if (last == 'Z')
                {
                    temp[i] = '0';
                    push(i - 1, temp[i - 1]);
                }
                else
                {
                    temp[i] = (char)++last;
                }
            }
            else
            {
                if (last == '9')
                {
                    temp[i] = 'A';
                }
                else
                {
                    temp[i] = (char)++last;
                }
            }
        }
 
判断多了点,我后来也看了别人是怎么实现的,貌似是将0-Z全都放到数组里面了,实现一个功能方法多的是,我的算法效率也不一定比别人高,但是我更想请看过的大神指点以下,能不能在我写的这个基础之上优化以下,我不喜欢定义个那么长的数组,然后一个个下边的去找呀!
 
最后通过控制台调用看一下效果吧。
 
     static void Main(string[] args)
        {
            for (int a = 0; a < 100; a++)
            {
                GetNextNo(ref value);
                Console.WriteLine(value);
            }
            Console.ReadLine();
        }
 获取下一个三十六进制数

你可能感兴趣的:(十六进制)