Tom
Tom

Reputation: 159

Simple binary to decimal converter - Issue in C#

I've made a simple binary to decimal converter, used to work fine, I used a string variable to declare the values, but now realise I will need to store them as integers as I wanted to add in a while loop structure to validate the users choice so they could only enter 0 or 1's.

But I now have a program which says Cannot convert from 'int' to 'System.IFormatProvider'.. As I am a beginner with C#,

I have no idea what this means, and how to over come the problem, any help appreciated.. Here is my code if anyone wants to look at it:

        int iBinaryNum; //To store binary number
        int iDecimalNum; //To store decimal numbers

        //Validation of user choice & main program
        while (iBinaryNum == 0 || iBinaryNum == 1)
        {
            Console.WriteLine("Enter the binary number you want to convert to decimal");
            iBinaryNum = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("The Binary number you have entered is " + iBinaryNum);
            iDecimalNum = Convert.ToInt32(iBinaryNum, 2);

            Console.WriteLine("This converted into decimal is " + iDecimalNum);
        }

        //If it's not equal to 0 or 1
        Console.WriteLine("Invalid binary number, Please re-enter");

        //Prevent program from closing
        Console.WriteLine("Press any key to close");
        Console.ReadKey();

Upvotes: 2

Views: 1730

Answers (5)

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

Complete Solution:

You can Use Regular Expressions to Check for Particular pattern in the Input String

The following program will take the Binary input from user and converts it into the decimal till the invalid value is found

       string strBinaryNum=""; //To store binary number
       int iDecimalNum; //To store decimal numbers
       System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex("^[0-1]+$");
            
        Console.WriteLine("Enter the binary number you want to convert to decimal");
        strBinaryNum = Console.ReadLine();
        while(r.Match(strBinaryNum).Success)
        {        
        Console.WriteLine("The Binary number you have entered is " + strBinaryNum);
        iDecimalNum = Convert.ToInt32(strBinaryNum, 2);            
        Console.WriteLine("This converted into decimal is " + iDecimalNum);

         Console.WriteLine("Enter the binary number you want to convert to decimal");
         strBinaryNum = Console.ReadLine();
        }
        Console.WriteLine("Press any key to close");
        Console.ReadKey();

Upvotes: 1

Lennart
Lennart

Reputation: 10343

Convert.ToInt32(String, Int32) requires the first argument to be a String and the second an integer. You are passing two ints, which resolves to Convert.ToInt32(Object, IFormatProvider) which generates the error. You have to convert the first argument (iBinaryNum) to a String.

But I don't think your code works as you expect, since the while condition only checks if either of the ints is either 1 or 0. If the user enters 1110 it fails. Also, if the user enters anything above Int32.Max (which wouldn't be too surprising, considering how large binary numbers can grow), your program crashes. I would store the user input in a string again and check each character whether it contains valid characters (1 or 0).

Like this:

bool IsBinaryNumber(string test){
        foreach(char c in test){
            // If c is not either 0 or 1, break.
            if(!((c=='0') || (c== '1'))){
                return false;
            }
        }
        // If everything went well, it's a binary number.
        return true;
 }

Upvotes: 2

Parrish Husband
Parrish Husband

Reputation: 3178

I'm not sure I really understand the problem. However here is a loop that will keep the only continue once the binary number requirements have been met.

uint iBinaryNum = 2; //To store binary number
decimal iDecimalNum; //To store decimal numbers

//Validation of user choice & main program
while (iBinaryNum > 1)
{
    Console.Write("Enter the binary number you want to convert to decimal: ");
    if (!uint.TryParse(Console.ReadLine(), out iBinaryNum) || iBinaryNum > 1)
    {
        //If it's not equal to 0 or 1
        Console.WriteLine("Invalid binary number, Please re-enter");
        iBinaryNum = 2;
    }
}

iDecimalNum = Convert.ToDecimal(iBinaryNum);
Console.WriteLine("This converted into decimal is " + iDecimalNum);

Upvotes: 0

Oded
Oded

Reputation: 499212

If you look at the different overload of Convert.ToInt32, you will see there are none that take as parameters Int32, Int32.

The overload resolution will select the one that takes object, IFormatProvider instead and 2 is an Int32, not a type that implements IFormatProvider, hence the error.

It is not clear why you are trying to convert an Int32 into an Int32 - you already have the value in iBinaryNum.

Upvotes: 0

Jeroen Vannevel
Jeroen Vannevel

Reputation: 44448

iDecimalNum = Convert.ToInt32(iBinaryNum, 2);

The arguments you passed are int, int. As you can see here, you can either choose between Object, IFormatProvider, String, IFormatProvider or String, Int32.

Since the first int can only be used as Object, this means the second argument has to be IFormatProvider.

If you want a solution, you'll have to clarify what it is you're trying to do. Why do you want to convert an integer to an integer?

Upvotes: 2

Related Questions