学习伴随一生
没有绝对,只有相对
/// <summary>
    /// 进制转化
    /// </summary>
    public class JZZH
    {
        string SeedStr = "";
        int SeedLen = 0;

        /// <summary>
        /// 默认36进制
        /// </summary>
        /// <param name="seed"></param>
        public JZZH(string seed = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")
        {
            SeedStr = seed;
            SeedLen = SeedStr.Length;
        }
        
        /// <summary>
        /// 编码
        /// </summary>
        /// <param name="val"></param>
        /// <returns></returns>
        public string Encode(int val)
        {
            string result = "";
            while (val >= SeedLen)
            {
                result = SeedStr[val % SeedLen] + result;
                val /= SeedLen;
            }
            if (val >= 0) result = SeedStr[val] + result;
            return result;
        }

        /// <summary>
        /// 解码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public int Decode(string str)
        {
            int result = 0;
            int len = str.Length;
            for (int i = len; i > 0; i--)
            {
                result += SeedStr.IndexOf(str[i - 1]) * Convert.ToInt32(Math.Pow(SeedLen, len - i));
            }
            return result;
        }
    }

 

posted on 2016-12-16 15:51  蒋正  阅读(163)  评论(0编辑  收藏  举报