首页 > 代码库 > 从M进制转换为N进制

从M进制转换为N进制

    /// <summary>
    /// 从M进制转换为N进制
    /// </summary>
    internal class MBase2NBase
    {
        /// <summary>
        /// M进制值字符串
        /// </summary>
        private string MValue = http://www.mamicode.com/"123";

        /// <summary>
        /// 除法商
        /// </summary>
        private string Quotient = null;

        /// <summary>
        /// M进制
        /// </summary>
        private int M = 10;

        /// <summary>
        /// N进制
        /// </summary>
        private int N = 8;

        /// <summary>
        /// .ctor
        /// </summary>
        public MBase2NBase()
        {
            
        }

        /// <summary>
        /// .ctor
        /// </summary>
        /// <param name="mValue">M进制值字符串</param>
        /// <param name="m">M进制(如10).参数值在2~36的范围内</param>
        /// <param name="n">N进制(如8),则意味着从10进制转换成8进制.参数值在2~36的范围内</param>
        public MBase2NBase(string mValue, int m, int n)
        {
            this.M = m;
            this.N = n;
            this.MValue =http://www.mamicode.com/ mValue;
        }

        /// <summary>
        /// 开始转换
        /// </summary>
        /// <param name="mValue">M进制值字符串</param>
        /// <param name="m">M进制(如10).参数值在2~36的范围内</param>
        /// <param name="n">N进制(如8),则意味着从10进制转换成8进制.参数值在2~36的范围内</param>
        /// <returns>N进制字符串</returns>
        public string Transform(string mValue, int m, int n)
        {
            this.M = m;
            this.N = n;
            this.MValue =http://www.mamicode.com/ mValue;
            return Transform();
        }

        /// <summary>
        /// M进制值字符串
        /// </summary>
        /// <returns>N进制字符串</returns>
        public string Transform()
        {
            if (M < 2 || M > 36)
            {
                throw new ArgumentOutOfRangeException("参数值不在2~36的范围内");
            }

            if (N < 2 || N > 36)
            {
                throw new ArgumentOutOfRangeException("参数值不在2~36的范围内");
            }

            if (M == N)
            {
                return MValue;
            }

            String nValue = "";
            Quotient = MValue;
            while (Quotient.Length > 0)
            {
                nValue = Remainder(Quotient) + nValue;
            }
return nValue; } /// <summary> /// 对给定的M进制字符串对n求余 /// </summary> /// <param name="mTempValue"></param> /// <returns></returns> private String Remainder(String mTempValue) { Quotient = ""; int temp = 0; while (mTempValue.Length > 0) { int t = GetIntFromStringFirstChar(mTempValue.Substring(0, 1)); mTempValue = mTempValue.Substring(1); temp = temp*M + t; Quotient += GetStrFromInt(temp / N); temp = temp%N; } while (Quotient.Length > 0 && Quotient[0] == 0) { Quotient = Quotient.Substring(1); } return GetStrFromInt(temp); } /// <summary> /// 字符串转换成Int /// </summary> /// <param name="str"></param> /// <returns></returns> private int GetIntFromStringFirstChar(String str) { return str[0] <= 9 && str[0] >= 0 ? str[0] - 0 : str[0] - a + 10; } /// <summary> /// Int转换为相应进制的字符 /// </summary> /// <param name="value"></param> /// <returns></returns> private String GetStrFromInt(int value) { String result = null; if (value >= 0 && value <= 9) result = ((char) (0 + value)).ToString(); else if (value > 9 && value < 36) { result = ((char) (a + value - 10)).ToString(); } else { throw new Exception("未知错误"); } return result; } }

 

从M进制转换为N进制