简体   繁体   English

C#中的十进制到二进制转换

[英]Decimal to Binary Conversion in C#

When you press button1 in should take the decimal number from textBox1 and display the binary number in textBox1. 当您按button1时,应从textBox1中获取十进制数字,并在textBox1中显示二进制数字。 Keep getting and error at the point of converting num to Int32. 在将num转换为Int32时不断出错。

private void button1_Click(object sender, EventArgs e)
    {
        int num;   // The number input into textBox1
        int quot;
        num = Convert.ToInt32(textBox1.Text);

        string rem;
        while(num > 1)
        {
            quot = num / 2;
            rem += (num % 2).ToString();
            num = quot;
        }
        string bin =" ";
        for (int i = rem.Length - 1; i >= 0; i--)
        {
            bin = bin + rem[i];
        }
        textBox1.Text = bin.ToString();
    }

You can simply use the Convert class 您可以简单地使用Convert类

string bin = Convert.ToString(num, 2);

see this method Convert.ToString Method (Int32, Int32) 请参见此方法Convert.ToString方法(Int32,Int32)

Converts the value of a 32-bit signed integer to its equivalent string representation in a specified base. 将32位带符号整数的值转换为指定基数的等效字符串表示形式。

http://msdn.microsoft.com/en-us/library/14kwkz77%28v=vs.110%29.aspx http://msdn.microsoft.com/zh-cn/library/14kwkz77%28v=vs.110%29.aspx

Edit: Similar question had been answered here Decimal to binary conversion in c # 编辑:这里已经回答了类似的问题c#中的十进制到二进制转换

Try this... 尝试这个...

private void button1_Click(object sender, EventArgs e)
        {

               double dbVlaue = Convert.ToDouble(textBox1.Text);
                int quot;
                int num;
                num = Convert.ToInt32(dbVlaue);

                string rem = string.Empty;
                while (num > 1)
                {
                    quot = num / 2;
                    rem += (num % 2).ToString();
                    num = quot;
                }
                string bin = " ";
                for (int i = rem.Length - 1; i >= 0; i--)
                {
                    bin = bin + rem[i];
                }
                textBox1.Text = bin.ToString();

    }
private void button1_Click(object sender, EventArgs e)
{

           double dbVlaue = Convert.ToDouble(textBox1.Text);
            int quot;
            int num;
            num = Convert.ToInt32(dbVlaue);

            string rem = string.Empty;
            while (num > 1)
            {
                quot = num / 2;
                rem += (num % 2).ToString();
                num = quot;
            }
            string bin = " ";
            for (int i = rem.Length - 1; i >= 0; i--)
            {
                bin = bin + rem[i];
            }
            textBox1.Text = bin.ToString();

}

"while (num > 1)" just change this line while (num >0) it will give a full answer. “ while(num> 1)”只需更改此行,而(num> 0)会给出完整的答案。 eg if you run this loop " while (num>1)" and you give any input value 8 to convert it into binary the answer must be 1000 but this program don't give you the out put of this program will be 000 last figure will not be shown because loop terminate it self but if you run this loop " while (num>0)" the result of your output is complete and correct the result of 8 will shown 1000 now program is complete for binary conversion all other code are right just make change in loop 例如,如果您运行此循环“ while(num> 1)”,并且给定任何输入值8以将其转换为二进制,则答案必须为1000,但此程序没有给出结果,该程序的输出将为000最后一位数字由于循环会自行终止,因此不会显示,但如果您在“((num> 0)”时运行此循环,则输出结果完成并更正结果8将显示1000,现在程序已完成二进制转换所有其他代码正确就可以进行循环更改

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

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