从M进制转换为N进制

Posted zslm___

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从M进制转换为N进制相关的知识,希望对你有一定的参考价值。

    /// <summary>
    /// 从M进制转换为N进制
    /// </summary>
    internal class MBase2NBase
    {
        /// <summary>
        /// M进制值字符串
        /// </summary>
        private string MValue = "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 = 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 = 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进制的主要内容,如果未能解决你的问题,请参考以下文章

将n进制数转换为m进制,但程序不正确。。

进制转换

给定一个十进制数M,以及需要转换的进制数N。 将十进制数M转化为N进制数

lua之m进制转换为n进制-任意进制转换算法

PAT A1015 Reversible Primes (20分) [素数 质数 进制转换]

n进制转成m进制的方法