【问题标题】:Why won't this binary to decimal converter in c# work? [duplicate]为什么这个二进制到十进制转换器在 c# 中不起作用? [复制]
【发布时间】:2013-11-26 21:42:11
【问题描述】:

好的,在尝试了在其他问题和谷歌上找到的大量解决方案后,我似乎无法找到解决这个问题的方法。这是我的代码,感谢任何帮助,它让我发疯,谢谢!

           int iBinaryNum; //To store binary number
           string sDecimalNum; //To store decimal numbers

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


           sDecimalNum = Convert.ToString(iBinaryNum, 2);

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

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

【问题讨论】:

  • 我没有在任何地方看到您的问题的描述。它有什么问题?
  • 如果我没记错的话,你的转换方式正好相反!尝试输入8,看看是否输出100
  • 它不会将二进制数转换为十进制数,它只是显示一个由 0 和 1 组成的奇怪数字,知道如何解决吗?抱歉简短。
  • 你在一小时前问过这个问题!!!这里:stackoverflow.com/questions/19967634/…>
  • @EugenRieck 是的,它输出了 1000,所以我看到这是 8 的二进制文件,真可惜,你确定根本没有办法以这种方式转换吗?多年来我一直在努力解决这个问题

标签: c# binary decimal converter


【解决方案1】:

你弄错了

    static void Main(string[] args)
    {
        string sBinaryNum; //To store binary number
        int iDecimalNum; //To store decimal numbers

        Console.WriteLine("Enter the binary number you want to convert to decimal");
        sBinaryNum = Console.ReadLine();

        Console.WriteLine("The Binary number you have entered is " + sBinaryNum);


        iDecimalNum = Convert.ToInt32(sBinaryNum, 2);

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

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

【讨论】:

  • 非常感谢!没有意识到它是如此简单,我什至没有意识到我有相反的方式,直到 eugen rieck 在上面指出它,我现在试试,不明白为什么它不起作用,非常感谢! :)
  • 如果对您有帮助,请接受他的回答。
  • 是的,我会的 :) 现在不让我,说还剩 6 分钟
猜你喜欢
  • 1970-01-01
  • 2020-12-07
  • 1970-01-01
  • 2016-01-17
  • 2013-05-14
  • 2021-11-08
  • 2020-07-06
  • 2016-05-18
  • 1970-01-01
相关资源
最近更新 更多