欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C# 二进制与十进制互转

程序员文章站 2022-07-15 09:34:58
...

直接贴代码:

            int mark = 19;
            int tem = ToErJin(mark);
            Console.WriteLine("转成二进制后:" + tem);  // 打印“转成二进制后:10011”

            int mark2 = 10011;
            int tem2 = ToShijin(mark2);
            Console.WriteLine("转成十进制后:" + tem2);// 打印“转成十进制后:19”

        /// <summary>
        /// 转换为二进制
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static int ToErJin(int value)
        {
            int temp = 0;
            int shang = 1;
            int yushu;
            while (shang != 0)
            {
                shang = (int)value / 2;
                yushu = value % 2;
                value = shang;
                temp += yushu;
                if (shang != 0)
                {
                    temp = temp * 10;
                }
            }
            //最后将 temp 倒序
            string tempStr = temp.ToString();
            int tempLength = tempStr.Length;
            string resultStr = string.Empty;
            for (int i = 0; i < tempLength; i++)
            {
                resultStr = tempStr[i] + resultStr;
            }
            return int.Parse(resultStr);
        }

        /// <summary>
        /// 转换为十进制(主要算法:个位数 * 2的零次方 + 十位数 * 2的一次方 + 百位数 * 2的二次方 + ...)
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static int ToShijin2(int value)
        {
            int temp = 0;
            int shang = value;
            int yushu;
            int mark = 0;
            while (shang != 0)
            {
                yushu = shang % 10;
                shang = shang / 10;
                temp += yushu * (int)Math.Pow(2, mark);
                mark++;
            }
            return temp;
        }

        /// <summary>
        /// 二进制转十进制的另一种方法(主要算法:1110111 = 1 * 2~6 + 1 * 2~5 + 1 * 2~4 + 0 * 2~3 + 1 * 2~2 + 1 * 2~1 + 1 * 2~0)
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static int ToShijin(int value)
        {
            string strValue = value.ToString();
            int valueLength = strValue.Length;
            int result = 0;
            for (int i = 0; i < valueLength; i++)
            {
                result += int.Parse(strValue[i].ToString()) * (int)Math.Pow(2, valueLength - 1 - i);
            }
            return result;
        }

谢谢浏览!

转载于:https://www.cnblogs.com/Music/archive/2011/04/25/binary-system-with-decimal-system-conversion-each-other.html