【问题标题】:Simple binary to decimal converter - Issue in C#简单的二进制到十进制转换器 - C# 中的问题
【发布时间】:2013-11-30 18:39:38
【问题描述】:

我制作了一个简单的二进制到十进制转换器,以前可以正常工作,我使用字符串变量来声明值,但现在意识到我需要将它们存储为整数,因为我想在 while 循环结构中添加验证用户的选择,以便他们只能输入 0 或 1。

但我现在有一个程序,上面写着Cannot convert from 'int' to 'System.IFormatProvider'.. 因为我是 C# 的初学者,

我不知道这意味着什么,以及如何解决这个问题,感谢任何帮助。如果有人想看,这是我的代码:

        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();

【问题讨论】:

  • 您的代码没有意义。
  • 为什么不呢?以前工作得很好,我曾经将 iBinaryNum 作为字符串 (sBinaryNum) 并且工作得很好,但是我需要验证用户的选择,所以我想也许将它转换为 int 可以让我这样做?那么哪一部分没有意义呢?
  • 我认为这里不需要while循环,你可以使用if然后无效的控制台消息语句应该在else块中
  • @Sudhakar 是的,我可以这样做,但真的不会有所作为吗?我会试一试,我需要先修复这条线“iDecimalNum = Convert.ToInt32(iBinaryNum,2);”,只是不确定如何,我不太明白答案中的人想要告诉什么我,我是编程新手

标签: c# loops while-loop int


【解决方案1】:
iDecimalNum = Convert.ToInt32(iBinaryNum, 2);

您传递的参数是int, int。如您所见here,您可以在Object, IFormatProviderString, IFormatProviderString, Int32 之间进行选择。

由于第一个 int 只能用作 Object,这意味着第二个参数必须是 IFormatProvider

如果你想要一个解决方案,你必须弄清楚你想要做什么。为什么要把整数转成整数?

【讨论】:

  • 我不太确定,我是 C# 的大菜鸟,有人告诉我这就是我要表示整数的方式,首先是转换它们,然后再使用它们.. 所以我猜这是错的?我只是希望能够将二进制数转换为小数,使用字符串可以正常工作,但是由于我无法使用诸如“||”之类的运算符而出现了问题在字符串中,有什么建议吗?谢谢
【解决方案2】:

Convert.ToInt32(String, Int32) 要求第一个参数是字符串,第二个参数是整数。您正在传递两个整数,它们解析为Convert.ToInt32(Object, IFormatProvider),从而产生错误。您必须将第一个参数 (iBinaryNum) 转换为字符串。

但我不认为您的代码按预期工作,因为 while 条件仅检查整数是否为 1 或 0。如果用户输入 1110,它将失败。此外,如果用户输入高于 Int32.Max 的任何内容(考虑到二进制数会增长到多大,这并不奇怪),您的程序就会崩溃。我会再次将用户输入存储在一个字符串中,并检查每个字符是否包含有效字符(1 或 0)。

像这样:

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;
 }

【讨论】:

  • Alrite 非常感谢,我现在试试,我看看你从哪里来,我会看看这个“String.Contains()”方法,没听说过之前,谢谢。
  • @Lennart:我不认为仅检查 o 或 1 并不能解决使用 Contains() 方法的问题,因为如果输入字符串包含类似 0178 的内容,它会成功,而且肯定不是如果我错了,请纠正我一个有效的二进制文件。
  • 这对我来说似乎是最好的选择。
  • 是的,我想是的,现在只是想在我的代码中实现它,谢谢大家的帮助,不胜感激
【解决方案3】:

完整解决方案:

您可以使用Regular ExpressionsInput String 中检查特定的pattern

下面的program 将从用户那里获取Binary 输入并将其转换为decimal,直到找到无效值

       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();

【讨论】:

  • 是的,谢谢,我会在之后解决这个问题,现在真的需要解决这个 int 问题,你有没有机会更好地解释其他人的意思?谢谢:)
  • @user2985995:当然我会帮助你,但首先你的 iBinaryNum 包含二进制值,对吗?例如:1100111,对吗?
  • 是的,用户输入一个二进制数,该二进制数存储在 iBinaryNum 中
  • 在这种情况下,您的 while 循环在大多数情况下可能会失败,因为用户只能输入 0 或 1,但不能输入 10,011,1100,否则会失败。
  • 是的,如果我实现 IF Else 语句,它会解决问题吗?
【解决方案4】:

如果您查看Convert.ToInt32 的不同重载,您会发现没有一个将Int32, Int32 作为参数。

重载决议将选择采用object, IFormatProvider 的那个,而2Int32,而不是实现IFormatProvider 的类型,因此会出现错误。

尚不清楚您为什么要尝试将Int32 转换为Int32 - 您已经拥有iBinaryNum 中的值。

【讨论】:

    【解决方案5】:

    我不确定我是否真的理解这个问题。然而,这里有一个循环,一旦满足二进制数要求,它将保持唯一的继续。

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

    【讨论】:

    • 非常感谢,我现在就试试
    • 哦,我想我完全错过了你想要做的事情。所以你想把像 11001001001 这样的东西转换成它的常规数值等价物?
    • 是的,只是喜欢一个十进制数,例如 111 将是 7 等的二进制等价物。
    猜你喜欢
    • 2021-01-03
    • 1970-01-01
    • 2013-05-14
    • 2019-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多