【问题标题】:Conversion between number systems hexToBin and binToHHex fails (C#)数字系统 hexToBin 和 binToHHex 之间的转换失败(C#)
【发布时间】:2015-12-06 06:34:35
【问题描述】:

我用 C# 编写了一个计算器。计算部分工作正常,我想添加一个在十进制、二进制和十六进制之间切换的函数。 为了转换显示(文本框)中的数字,我做了几个方法。 6 个作品中的 4 个。 hexToBin 和 binToHex 不工作。文本框仍然显示第一个基数中的数字。但是,请注意,该程序不会失败。当我在 bin 中输入一个数字时,(尝试)将其转换为十六进制,然后选择 dec,它会正确显示十进制数字。

编辑:在此开关中,我尝试找出问题所在。我发现 MessageBoxes 永远不会显示。案例“H”从来都不是真的吗?

        switch (talbas)
        {
            case 'D':
                {
                    //Dec till Bin
                    output = decTillBin(textBoxDisplay.Text);
                }
                break;
            case 'H':
                {
                    MessageBox.Show("Hex till Bin");
                    //Hex till Bin
                    output = hexTillBin(textBoxDisplay.Text);
                    MessageBox.Show(output);
                }
                break;
        }

这是单选按钮的代码。 (评论和名字都是瑞典语,但我想你会明白的)¨

 private void radioButtonDec_CheckedChanged(object sender, EventArgs e)
    {

        if(radioButtonHex.Checked)
            {
                button0.Enabled = true;
                button1.Enabled = true;
                button2.Enabled = true;
                button3.Enabled = true;
                button4.Enabled = true;
                button5.Enabled = true;
                button6.Enabled = true;
                button7.Enabled = true;
                button8.Enabled = true;
                button9.Enabled = true;
                buttonA.Enabled = true;
                buttonB.Enabled = true;
                buttonC.Enabled = true;
                buttonD.Enabled = true;
                buttonE.Enabled = true;
                buttonF.Enabled = true;
                buttonDeci.Enabled = false;
                buttonTeckenByte.Enabled = false;
                switch (talbas)
                {
                    case 'D': 
                        {
                            //Dec till Hex
                            output = decTillHex(textBoxDisplay.Text);
                        }
                        break;
                    case 'B': 
                        {
                            //Bin till Hex
                            output = binTillHex(textBoxDisplay.Text);
                        }
                        break;
                }
                textBoxDisplay.Text = output;
                talbas = 'H';
            }
        else if (radioButtonBin.Checked)
        {
            button0.Enabled = true;
            button1.Enabled = true;
            button2.Enabled = false;
            button3.Enabled = false;
            button4.Enabled = false;
            button5.Enabled = false;
            button6.Enabled = false;
            button7.Enabled = false;
            button8.Enabled = false;
            button9.Enabled = false;
            buttonA.Enabled = false;
            buttonB.Enabled = false;
            buttonC.Enabled = false;
            buttonD.Enabled = false;
            buttonE.Enabled = false;
            buttonF.Enabled = false;
            buttonDeci.Enabled = false;
            buttonTeckenByte.Enabled = false;

            switch (talbas)
            {
                case 'D':
                    {
                        //Dec till Bin
                        output = decTillBin(textBoxDisplay.Text);
                    }
                    break;
                case 'H':
                    {
                        MessageBox.Show("Hex till Bin");
                        //Hex till Bin
                        output = hexTillBin(textBoxDisplay.Text);
                        MessageBox.Show(output);
                    }
                    break;
            }
            textBoxDisplay.Text = output;
            talbas = 'B';
        }
        else if(radioButtonDec.Checked)
        {
            if (talbas == 'B')
            {
                //Bin till Dec
                output = binTillDec(textBoxDisplay.Text);
            }
            else if (talbas == 'H')
            {
                //Hex till Dec
                output = hexTillDec(textBoxDisplay.Text);
            }

                textBoxDisplay.Text = output;
                button0.Enabled = true;
                button1.Enabled = true;
                button2.Enabled = true;
                button3.Enabled = true;
                button4.Enabled = true;
                button5.Enabled = true;
                button6.Enabled = true;
                button7.Enabled = true;
                button8.Enabled = true;
                button9.Enabled = true;
                buttonA.Enabled = false;
                buttonB.Enabled = false;
                buttonC.Enabled = false;
                buttonD.Enabled = false;
                buttonE.Enabled = false;
                buttonF.Enabled = false;
                buttonDeci.Enabled = true;
                talbas = 'D';
        }
        textBoxDisplay.Text = output;

    }

方法如下:

    //Byt talsystem
    private string binTillDec(string input)
    {
        //Bin till dec
        decInt = Convert.ToInt32(input, 2);
        decString = decInt.ToString();
        string returnValue = decString;

        return(returnValue);
    }

    private string hexTillDec(string input)
    {
        //Hex till Dec
        decInt = Convert.ToInt32(input, 16);
        decString = decInt.ToString();
        string returnValue = decString;

        return (returnValue);
    }

    private string decTillBin(string input)
    {
        //Dec till Bin
        decInt = int.Parse(input);
        binString = Convert.ToString(decInt, 2);
        string returnValue = binString;

        return (returnValue);
    }

    private string decTillHex(string input)
    {
        //Dec till Hex
        decInt = int.Parse(input);
        hexString = decInt.ToString("X");
        string returnValue = hexString; ;

        return (returnValue);
    }

    private string binTillHex(string input)
    {
        //Bin till Hex

        //Bin till dec
        decInt = Convert.ToInt32(input, 2);
        //Dec till Hex
        hexString = decInt.ToString("X");
        string returnValue = hexString;

        return (returnValue);
    }

    private string hexTillBin(string input)
    {
        //Hex till Bin

        //Hex till Dec
        decInt = Convert.ToInt32(input, 16);
        //Dec till Bin
        binString = Convert.ToString(decInt, 2);
        string returnValue = binString;

        return (returnValue);
    }

【问题讨论】:

  • 您的 UI 代码在这里并不真正相关,假设它实际上是转换问题,而不是 UI(您应该首先使用调试器进行适当的研究以检查哪些情况是正确的)。更有用的是一个简短但完整的程序来演示该问题,包括硬编码的样本输入,然后是预期输出与实际输出……您目前还没有真正描述出了什么问题。
  • talbas 的值是多少?您可以对其进行调试以检查其真实情况(而不是您期望的情况)吗?
  • 当我单步调试调试器时,我意识到当单选按钮将状态从 bin 更改为 hex 时,根本没有发生任何事情,反之亦然。似乎它甚至没有进入开关。
  • 我明白了!我只是检查 decButtons 状态是否已更改。当从 bin 到 hex 或 hex 到 bin dec 完全不受影响时,这就是为什么没有发生任何事情

标签: c# binary hex


【解决方案1】:

用于在任意基数(从 2 到 36)之间转换数字的库类,请参阅 this page

希望对你有帮助。

【讨论】:

  • 这是一个仅链接的答案 - 在 Stack Overflow 上不鼓励这样做,因为链接可能会随着时间的推移而失效,并且如果不访问目标页面,答案实际上没有任何信息。
猜你喜欢
  • 2014-01-05
  • 2016-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-13
相关资源
最近更新 更多