十进制转62进制

来源:互联网 发布:westvleteren 12 淘宝 编辑:程序博客网 时间:2024/04/30 15:00
方法一,
long s = 99999999999999;
string s1 = Base62Encode(s);

long s2 = Base62Decode(s1);

    

        static string Base62Encode(long num)
        {
            string s = "";
            while (num != 0)
            {
                s = tenValue2Char(num % 62).ToString() + s;
                num = num / 62;
            }
            return s;
        }


        static string tenValue2Char(long ten)
        {
            if (ten < 10) return ten.ToString();
            if (ten >= 10 && ten <= 36) return Convert.ToChar(ten + 87).ToString();
            if (ten >= 37 && ten <= 61) return Convert.ToChar(ten - 29).ToString();
            return "";
        }
        static long Base62Decode(string hex)
        {
            long l = 0;
            for (int i = 0; i < hex.Length; i++)
            {
                l = l + (long)HexChar2Value(hex[i]) * (long)Math.Pow((double)62, (double)(hex.Length - i - 1));
            }
            return l;
        }


        static int HexChar2Value(char hexChar)
        {
            if (hexChar >= 97 && hexChar <= 122) return hexChar - 87;
            if (hexChar >= 65 && hexChar <= 90) return hexChar - 29;
            return Convert.ToInt32(hexChar.ToString());
        }


方法二,
long s = 99999999999999;
string s1 = Base62Encode(s);

long s2 = Base62Decode(s1);


 static string Base62Encode(long num)        {            string s = "";            while (num != 0)            {                s = tenValue2Char(num % 62).ToString() + s;                num = num / 62;            }            return s;        }        static string tenValue2Char(long 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 "x";                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 "X";                case 60: return "Y";                case 61: return "Z";                default: return "";            }        }        static long Base62Decode(string hex)        {            long l = 0;            for (int i = 0; i < hex.Length; i++)            {                l = l + (long)HexChar2Value(hex[i]) * (long)Math.Pow((double)62, (double)(hex.Length - i - 1));            }            return l;        }        static int HexChar2Value(char hexChar)        {            switch (hexChar)            {                case '0':                case '1':                case '2':                case '3':                case '4':                case '5':                case '6':                case '7':                case '8':                case '9':                    return Convert.ToInt32(hexChar.ToString());                case 'a': return 10;                case 'b': return 11;                case 'c': return 12;                case 'd': return 13;                case 'e': return 14;                case 'f': return 15;                case 'g': return 16;                case 'h': return 17;                case 'i': return 18;                case 'j': return 19;                case 'k': return 20;                case 'l': return 21;                case 'm': return 22;                case 'n': return 23;                case 'o': return 24;                case 'p': return 25;                case 'q': return 26;                case 'r': return 27;                case 's': return 28;                case 't': return 29;                case 'u': return 30;                case 'v': return 31;                case 'w': return 32;                case 'x': return 33;                case 'y': return 34;                case 'z': return 35;                case 'A': return 36;                case 'B': return 37;                case 'C': return 38;                case 'D': return 39;                case 'E': return 40;                case 'F': return 41;                case 'G': return 42;                case 'H': return 43;                case 'I': return 44;                case 'J': return 45;                case 'K': return 46;                case 'L': return 47;                case 'M': return 48;                case 'N': return 49;                case 'O': return 50;                case 'P': return 51;                case 'Q': return 52;                case 'R': return 53;                case 'S': return 54;                case 'T': return 55;                case 'U': return 56;                case 'V': return 57;                case 'W': return 58;                case 'X': return 59;                case 'Y': return 60;                case 'Z': return 61;                default:                    return 0;            }        }


方法三,System.Linq

long s = 99999999999999;
string s1 = Base62Encode(s);

long s2 = Base62Decode(s1);


 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;
        }

原创粉丝点击