64进制和10进制转换

class Program
    {
        static void Main(string[] args)
        {
            long val = 123456789;
            Console.WriteLine(Parse64Encode(val));
            Console.WriteLine(Parse64Decode(Parse64Encode(val)));
            Console.ReadKey();
        }

        private static char[] rDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '+', '/' };
        /// <summary>
        /// long转64进制
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string Parse64Encode(long value)
        {
            int digitIndex = 0;
            long longPositive = Math.Abs(value);
            int radix = 64;//64进制
            char[] outDigits = new char[65];
            for (digitIndex = 0; digitIndex <= 64; digitIndex++)
            {
                if (longPositive == 0) { break; }
                outDigits[outDigits.Length - digitIndex - 1] = rDigits[longPositive % radix];
                longPositive /= radix;
            }
            return new string(outDigits, outDigits.Length - digitIndex, digitIndex);
        }

        /// <summary>
        /// 64进制转long
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private static long Parse64Decode(string value)
        {
            int fromBase = 64;
            value = value.Trim();
            if (string.IsNullOrEmpty(value))
            {
                return 0L;
            }
            string sDigits = new string(rDigits, 0, fromBase);
            long result = 0;
            for (int i = 0; i < value.Length; i++)
            {
                if (!sDigits.Contains(value[i].ToString()))
                {
                    throw new ArgumentException(string.Format("The argument \"{0}\" is not in {1} system", value[i], fromBase));
                }
                else
                {
                    try
                    {
                        int index = 0;
                        for (int xx = 0; xx < rDigits.Length; xx++)
                        {
                            if (rDigits[xx] == value[value.Length - i - 1])
                            {
                                index = xx;
                            }
                        }
                        result += (long)Math.Pow(fromBase, i) * index;//   2
                    }
                    catch
                    {
                        throw new OverflowException("运算溢出");
                    }
                }
            }
            return result;
        }
    }

  

posted @ 2015-05-13 18:56  晓风拂月  阅读(6569)  评论(1编辑  收藏  举报