简体   繁体   English

C#System.OverflowException二进制到十进制和十六进制转换

[英]C# System.OverflowException binary to decimal and hex conversion

I am currently building ac# winform application to convert input from three textboxes, one decimal, one hex and one binary. 我目前正在构建ac#winform应用程序,以转换来自三个文本框,一个十进制,一个十六进制和一个二进制的输入。 So you would enter your decimal number in the decimal textbox and the equivalent hex and binary numbers would appear in their respective text boxes. 因此,您将在十进制文本框中输入十进制数字,等效的十六进制和二进制数字将出现在它们各自的文本框中。 Here is the code I used for the decimal and hex text boxes for the conversions. 这是我用于转换的十进制和十六进制文本框的代码。

  private void textBox1_TextChanged(object sender, EventArgs e)
    {
        //conversion to hex and binary for the other texbox's

        if (button20WasClicked == false) { 
           long x = Int64.Parse(textBox1.Text);
           textBox2.Text = Convert.ToString(x,16).ToUpper();

           long y = Int64.Parse(textBox1.Text);
           textBox3.Text = Convert.ToString(y,2);

       }
        else
        {

        }
        button20WasClicked = false;
    }


 private void textBox2_TextChanged(object sender, EventArgs e)
    {

        if (button20WasClicked == false)
        {
            long x = Int64.Parse(textBox2.Text, System.Globalization.NumberStyles.HexNumber);
            textBox1.Text = Convert.ToString(x, 10);

            long y = Int64.Parse(textBox2.Text, System.Globalization.NumberStyles.HexNumber);
            textBox3.Text = Convert.ToString(y, 2);

        }
        else
        {

        }
        button20WasClicked = false;
    }

Now these worked like a dream for converting decimal to hex and binary (textBox1) and hex to decimal and binary (textBox2) but when I try a similar approach on the textbox for inputing binary to convert to decimal and hex I keep getting an overflow exception was unhandled error, value was either too large or small for Int 64. I have tried changing to other size variables but get the same error. 现在这些工作就像梦想一样,将十进制转换为十六进制和二进制(textBox1),并将十六进制转换为十进制和二进制(textBox2),但是当我在文本框上尝试类似的方法以将二进制输入转换为十进制和十六进制时,我不断收到溢出异常是未处理的错误,对于Int 64而言,值太大或太小。我尝试更改为其他大小变量,但得到相同的错误。 Everything I can find on the internet suggests I am on the right track with this .Convert approach but just can't get anything to work. 我在互联网上可以找到的所有内容都表明我使用这种.convert方法是正确的,但是什么也做不了。 Here is the code for the final text box causing the error. 这是导致错误的最终文本框的代码。

  private void textBox3_TextChanged_1(object sender, EventArgs e)
    {
        if (button20WasClicked == false)
        {
            long x = Int64.Parse(textBox3.Text);
            textBox1.Text = Convert.ToString(x, 10);

            long y = Int64.Parse(textBox3.Text);
            textBox2.Text = Convert.ToString(y, 16);

        }
        else
        {

        }
        button20WasClicked = false;
    }

Any help appreciated as I have been working on this problem for two days now. 感谢您的帮助,因为我已经为该问题解决了两天。

You should not use Parse methods. 您不应该使用Parse方法。

When you convert long to string, you correctly use Convert.ToString overload that allows specifying a base 当您将long转换为字符串时,可以正确使用Convert.ToString重载,该重载允许指定基数

public static string ToString(long value, int toBase) 公共静态字符串ToString(long value,int toBase)

What you are missing is the corresponding reverse Convert.ToInt64 method 您缺少的是相应的反向Convert.ToInt64方法

static long ToInt64(string value, int fromBase) 静态长ToInt64(字符串值,int fromBase)

Shortly, change your parsing code in the three places respectively to 很快,请分别在三个位置将解析代码更改为

long x = Convert.ToInt64(textBox1.Text, 10);
long x = Convert.ToInt64(textBox2.Text, 16);
long x = Convert.ToInt64(textBox3.Text, 2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM