我如何...以十进制转换较大的二进制数 [英] How do I... Convert larger binary number in decimal

查看:120
本文介绍了我如何...以十进制转换较大的二进制数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码工作得非常好但是当我尝试将超过10位的二进制(例如1100101010)转换为十进制时,它会给我一个值太大的错误



此处代码



The code was working very good but then When i try to convert more then 10 digits of binary (e.g 1100101010) into decimal it give me the error that value is too large

here the Code

First File.cs
namespace ConsoleApplication1
{
    public class ConvertBinaryToHex    // this code is a procedure, i need it in objects
    {
        public String _BinToDec(int binaryNumber)
        {
            int decimalNumber = 0;
            int power = 0;


            if (binaryNumber == 0000)  // do you know how can I make just one return?(Below there is a return) and not 2. I mean this decision but without use return.
            {
                return "0";  
            }
            else
            {


                while (binaryNumber != 0)
                {
                    int aDecimalDigit;
                    aDecimalDigit = (binaryNumber % 10);

                    int aDecimalRisesToPower;
                    aDecimalRisesToPower = (int)Math.Pow(2, power);

                    int risesADecimalToAConsecutivePower = 0;
                    risesADecimalToAConsecutivePower = aDecimalDigit * aDecimalRisesToPower;

                    decimalNumber = decimalNumber + risesADecimalToAConsecutivePower;
                    binaryNumber = binaryNumber / 10;

                    power++;
                }

                String conversion = "  ";
                int theResidue;

                while (decimalNumber != 0)
                {
                    theResidue = decimalNumber % 16;
                    decimalNumber = decimalNumber / 16;

                    if (theResidue == 10)
                        conversion += "A";
                    else if (theResidue == 11)
                        conversion += "B";
                    else if (theResidue == 12)
                        conversion += "C";
                    else if (theResidue == 13)
                        conversion += "D";
                    else if (theResidue == 14)
                        conversion += "E";
                    else if (theResidue == 15)
                        conversion += "F";
                    else
                        conversion += theResidue;
                }

                // Para voltear el string
                char[] inputarray = conversion.ToCharArray();
                Array.Reverse(inputarray);
                return new string(inputarray); // this return will be ok, the first you have to change the logic. just this return in the code
            }
        }
    }
}
Program.cs
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            ConvertBinaryToHex   object1 = new ConvertBinaryToHex();

            //send this binary number as a parameter 

            //110101111101111000011010
            int a ;
            Console.WriteLine("please write 10 digit binary number : \a");
            // Convert.ToInt64(bno);
            a=Convert.ToInt32(Console.ReadLine());
            Console.WriteLine(object1._BinToDec(a));



            
        }
    }
}





我尝试了什么:



我尝试使用(long,Convert.ToInt64,ulong,uint)二进制和十进制但它没有用。



What I have tried:

I tried using ( long,Convert.ToInt64,ulong,uint ) with binary and decimal but it did not work.

推荐答案

Woha,这是一个奇怪的。



我想你将二进制表示为十进制。那就是1100101010实际上是11亿,而不是810,正如你所料。你正在接近使用你的整数的所有32位。



使用类似字符串的东西代表你的二进制文件更有意义。
Woha, this is a weird one.

I think you are representing your binary as decimal. That is 1100101010 is actually 1.1 billion, not 810 as you might expect. You are getting close to using all 32 bits of your integer.

It would make more sense to use something like a string to represent your binary.


问题是110101111101111000011010不是有效的十进制数 - 它是一个有效的二进制数,但它的方式,大于适合整数的最大值:2,147,483,647

所以当你这样做时:

The problem is that "110101111101111000011010" isn't a valid base ten number - it's a valid binary number, but it's way, way bigger than the largest value that will fit in an integer: 2,147,483,647
So when you do this:
a=Convert.ToInt32(Console.ReadLine());

它试图将其转换为十进制(基数为10)并溢出。

如果你想将它作为二进制数,那么使用重载:

It tries to convert it as a decimal (base 10) number and overflows.
If you want it as a binary number, then use a overload:

a=Convert.ToInt32(Console.ReadLine(),2);

但要注意用户会犯错误:单个字符错误会导致应用程序崩溃。

But do be aware that users make mistakes: and a single character wrong will crash your application.


这篇关于我如何...以十进制转换较大的二进制数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆