0

When you press button1 in should take the decimal number from textBox1 and display the binary number in textBox1. Keep getting and error at the point of converting num to 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();
    }
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Nathan Thompson
  • 77
  • 2
  • 11
  • 2
    What is the error exactly? On which line? What is the value of `textBox1.Text` and what is your `CurrentCulture`? – Soner Gönül Oct 21 '14 at 05:59
  • "Keep getting and error at the point of converting num to Int32" Where exactly are you doing this conversion? – Leo Oct 21 '14 at 06:01
  • 1
    Aside from anything else, this wouldn't compile because `rem` isn't definitely assigned. – Jon Skeet Oct 21 '14 at 06:03
  • Maybe you just need sth like `textBox1.Text.Trim()` or check if `textBox1.Text` is not null or empty. –  Oct 21 '14 at 06:21

3 Answers3

1

You can simply use the Convert class

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

see this method Convert.ToString Method (Int32, Int32)

Converts the value of a 32-bit signed integer to its equivalent string representation in a specified base.

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

Edit: Similar question had been answered here Decimal to binary conversion in c #

Community
  • 1
  • 1
palazzo train
  • 3,229
  • 1
  • 19
  • 40
1

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

    }
Kumod Singh
  • 2,113
  • 1
  • 16
  • 18
0
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. e.g 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

BenMorel
  • 34,448
  • 50
  • 182
  • 322