解决方案 »

  1.   

    可行,只要目标编码的取值范围大于原来的编码就可以。比如000000000000 -- 999999999999是1000亿
    那么如果你用1-9 a-z A-Z编码,可以缩短到7个字符长度。最简单的是使用base62编码。也就是将十进制数字转换成62进制数字,对于每一位,如果是0,就表示为0,1表示为1,9表示为9,10表示为a,...35表示为z,36表示为A,...61表示为Z。你可以参考标准的base64编码,得到你的base62编码。
      

  2.   

    static string L2S(long l)
    {
        byte[] bytes = BitConverter.GetBytes(l).Reverse().SkipWhile(b => b == 0).ToArray();
        return Convert.ToBase64String(bytes).Replace("=","");
    }
    static long S2L(string s)
    {
        s = s.PadRight((s.Length + 3) / 4 * 4, '=');
        byte[] bytes = Convert.FromBase64String(s).Reverse().Concat(new byte[8]).Take(8).ToArray();
        return BitConverter.ToInt64(bytes, 0);
    }
    static void Main(string[] args)
    {
        string s1 = L2S(999999999999);  // 6NSlD/8
        long l1 = S2L(s1);              // 999999999999    string s2 = L2S(123);           // ew
        long l2 = S2L(s2);              // 123
    }
      

  3.   

    这样的话。我也想过。如果是还是要解决0-99的对应问题,我做出来过,有重复的这样做肯定可以,有问题也是你代码的问题。
    用了base 64  编码,输入12位数字,出来15位编码,更长了
      

  4.   


    用了base64编码,测试结果,输入12位数字,转换成了15位编码,更长了。我要缩短啊
      

  5.   

    这样的话。我也想过。如果是还是要解决0-99的对应问题,我做出来过,有重复的这样做肯定可以,有问题也是你代码的问题。
    用了base 64  编码,输入12位数字,出来15位编码,更长了我能说你什么好呢。
      

  6.   


    用了base64编码,测试结果,输入12位数字,转换成了15位编码,更长了。我要缩短啊实在不知说什么好了,你看7楼都帮你写好了:
        string s1 = L2S(999999999999);  // 6NSlD/8
        long l1 = S2L(s1);              // 99999999999912位数字转成了7位编码。
      

  7.   

       public string Ten2Hex(string ten)
            {
                ulong tenValue = Convert.ToUInt64(ten);
                ulong divValue, resValue;
                string hex = "";
                do
                {
                    divValue = (ulong)Math.Floor(Convert.ToDouble(tenValue / 62));
                    resValue = tenValue % 62;
                    hex = tenValue2Char(resValue) + hex;
                    tenValue = divValue;
                }
                while (tenValue >= 62);
                if (tenValue != 0)
                    hex = tenValue2Char(tenValue) + hex;
                return hex;
            }        public string tenValue2Char(ulong ten)
            {
                switch (ten)
                {
                    case 0:
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                    case 5:
                    case 6:
                    case 7:
                    case 8:
                    case 9:
                        return ten.ToString();
                    case 10: return "a";
                    case 11: return "b";
                    case 12: return "c";
                    case 13: return "d";
                    case 14: return "e";
                    case 15: return "f";
                    case 16: return "g";
                    case 17: return "h";
                    case 18: return "i";
                    case 19: return "j";
                    case 20: return "k";
                    case 21: return "l";
                    case 22: return "m";
                    case 23: return "n";
                    case 24: return "o";
                    case 25: return "p";
                    case 26: return "q";
                    case 27: return "r";
                    case 28: return "s";
                    case 29: return "t";
                    case 30: return "u";
                    case 31: return "v";
                    case 32: return "w";
                    case 33: return "s";
                    case 34: return "y";
                    case 35: return "z";
                    case 36: return "A";
                    case 37: return "B";
                    case 38: return "C";
                    case 39: return "D";
                    case 40: return "E";
                    case 41: return "F";
                    case 42: return "G";
                    case 43: return "H";
                    case 44: return "I";
                    case 45: return "J";
                    case 46: return "K";
                    case 47: return "L";
                    case 48: return "M";
                    case 49: return "N";
                    case 50: return "O";
                    case 51: return "P";
                    case 52: return "Q";
                    case 53: return "R";
                    case 54: return "S";
                    case 55: return "T";
                    case 56: return "U";
                    case 57: return "V";
                    case 58: return "W";
                    case 59: return "S";
                    case 60: return "Y";
                    case 61: return "Z";
                    default: return "";
                }
            }            MessageBox.Show(Ten2Hex("999999999999"));
    结果:hBsM5A3
      

  8.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;namespace ConsoleApplication1
    {
        class Program
        {
            static string Base62Encode(long num)
            {
                string s = "";
                char[] metachar = Enumerable.Range(0, 10).Select(x => (char)('0' + x))
                    .Concat(Enumerable.Range(0, 26).Select(x => (char)('a' + x)))
                    .Concat(Enumerable.Range(0, 26).Select(x => (char)('A' + x)))
                    .ToArray();
                while (num != 0)
                {
                    s = metachar[num % 62].ToString() + s;
                    num = num / 62;
                }
                return s;
            }        static long Base62Decode(string s)
            {
                var dict = Enumerable.Range(0, 10).Select(x => (char)('0' + x))
                    .Concat(Enumerable.Range(0, 26).Select(x => (char)('a' + x)))
                    .Concat(Enumerable.Range(0, 26).Select(x => (char)('A' + x)))
                    .Select((x, i) => new { x, i }).ToDictionary(x => x.x, x => x.i);
                long l = 0;
                for (int i = 0; i < s.Length; i++)
                {
                    l = l + (long)dict[s[i]] * (long)Math.Pow((double)62, (double)(s.Length - i - 1));
                }
                return l;
            }        static void Main(string[] args)
            {
                long l = 999999999999;
                string s = Base62Encode(l);
                Console.WriteLine(s);
                Console.WriteLine(Base62Decode(s));
            }
        }
    }
    hBxM5A3
    999999999999
    Press any key to continue . . .