码迷,mamicode.com
首页 > 其他好文 > 详细

从M进制转换为N进制

时间:2017-04-05 13:48:20      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:orm   har   tor   sub   进制   8进制   stc   nbsp   tin   

    /// <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进制

标签:orm   har   tor   sub   进制   8进制   stc   nbsp   tin   

原文地址:http://www.cnblogs.com/zhshlimi/p/6668254.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!