Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
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));



            
        }
    }
}


What I have tried:

I tried using ( long,Convert.ToInt64,ulong,uint ) with binary and decimal but it did not work.
Posted
Updated 17-Mar-16 6:21am
Comments
Patrice T 17-Mar-16 12:10pm    
Looks like you are complicated in your head.
You should rather explain what you want to do and maximum values.
I think you do it wrong from the core. you choosen a wrong way to do things and then it get more complicated as you do things.
Member 12245428 18-Mar-16 2:41am    
My program is working with 10 digit value but when i insert bigger value then 10 digits in binary it do not convert the value

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.
 
Share this answer
 
Comments
Patrice T 17-Mar-16 12:23pm    
Does it count as a solution ?
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:
C#
a=Convert.ToInt32(Console.ReadLine());
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:
C#
a=Convert.ToInt32(Console.ReadLine(),2);
But do be aware that users make mistakes: and a single character wrong will crash your application.
 
Share this answer
 
Comments
Member 12245428 18-Mar-16 2:42am    
the program working good until i insert bigger digit then 10 and it is converting binary into decimal

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900