Decimal Number to Binary String

Lardmeister 0 Tallied Votes 374 Views Share

I could not resist to put this little utility into a GUI dress. The code shows you that the C# StringBuilder is nicely suited to create the binary string. Also included is a check to assure that the input is an all digit numeric string.

// give the binary representation of a decimal number
// uses a Windows GUI with Form, Label, TextBox, and Button components

using System;
using System.Windows.Forms;  // GUI things
using System.Text;           // StringBuilder

namespace WindowsApplicationDec2Bin
{
  public class Form1 : System.Windows.Forms.Form
  {
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.TextBox textBox2;
    private System.ComponentModel.Container components = null;

    public Form1()
    {
      InitializeComponent();
    }

    protected override void Dispose( bool disposing )
    {
      if ( disposing )
      {
        if (components != null) 
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }

    #region Windows Form Designer generated code
    private void InitializeComponent()
    {
      this.label1 = new System.Windows.Forms.Label();
      this.textBox1 = new System.Windows.Forms.TextBox();
      this.button1 = new System.Windows.Forms.Button();
      this.label2 = new System.Windows.Forms.Label();
      this.textBox2 = new System.Windows.Forms.TextBox();
      this.SuspendLayout();
      // 
      // label1
      // 
      this.label1.BackColor = System.Drawing.Color.Bisque;
      this.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
      this.label1.Location = new System.Drawing.Point(16, 16);
      this.label1.Name = "label1";
      this.label1.Size = new System.Drawing.Size(120, 16);
      this.label1.TabIndex = 0;
      this.label1.Text = "Enter integer number:";
      // 
      // textBox1
      // 
      this.textBox1.BackColor = System.Drawing.Color.Yellow;
      this.textBox1.Location = new System.Drawing.Point(152, 16);
      this.textBox1.Name = "textBox1";
      this.textBox1.Size = new System.Drawing.Size(120, 20);
      this.textBox1.TabIndex = 1;
      this.textBox1.Text = "1";
      // 
      // button1
      // 
      this.button1.BackColor = System.Drawing.Color.Silver;  //PeachPuff;
      this.button1.Location = new System.Drawing.Point(16, 48);
      this.button1.Name = "button1";
      this.button1.Size = new System.Drawing.Size(256, 23);
      this.button1.TabIndex = 2;
      this.button1.Text = "Press to calculate binary number";
      this.button1.Click += new System.EventHandler(this.button1_Click);
      // 
      // label2
      // 
      this.label2.BackColor = System.Drawing.Color.Bisque;
      this.label2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
      this.label2.Location = new System.Drawing.Point(16, 88);
      this.label2.Name = "label2";
      this.label2.Size = new System.Drawing.Size(104, 16);
      this.label2.TabIndex = 3;
      this.label2.Text = "The binary result:";
      // 
      // textBox2
      // 
      this.textBox2.BackColor = System.Drawing.Color.PaleGreen;
      this.textBox2.Location = new System.Drawing.Point(16, 118);
      this.textBox2.Name = "textBox2";
      this.textBox2.Size = new System.Drawing.Size(232, 20);
      this.textBox2.TabIndex = 4;
      this.textBox2.Text = "";
      // 
      // Form1
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.BackColor = System.Drawing.Color.DarkGreen;
      this.ClientSize = new System.Drawing.Size(284, 162);
      this.Controls.Add(this.textBox2);
      this.Controls.Add(this.label2);
      this.Controls.Add(this.button1);
      this.Controls.Add(this.textBox1);
      this.Controls.Add(this.label1);
      this.Name = "Form1";
      this.Text = "Decimal to Binary";
      this.ResumeLayout(false);
    }
    #endregion

    static void Main() 
    {
      Application.Run(new Form1());
    }

    // return true if all of the characters in the string are digits
    public static bool IsAllDigits(string sRaw)
    {
      string s = sRaw.Trim();
      if (s.Length == 0)
      {
        return false;
      }
      for(int index = 0; index < s.Length; index++)
      {
        if (Char.IsDigit(s[index]) == false)
        {
          return false;
        }
      }
      return true;
    }

    private void button1_Click(object sender, System.EventArgs e)
    {
      StringBuilder sb1 = new StringBuilder();
      string s;
      int deci = 0;
      int remain;
      
      // check if entry has all digits
      s = this.textBox1.Text;
      if (IsAllDigits(s)) 
        deci = Int32.Parse(s);
      else
      {
        this.textBox1.Text += " Integer only!";
        this.textBox1.Focus();
      }
        
      // build the binary string
      if (deci > 0)
      {
        do
        {
          remain = deci % 2;
          deci   = deci / 2;
          sb1.Insert(0, remain);
        } while(deci > 0);
      }
      this.textBox2.Text = sb1.ToString();
    }
  }
}
Lardmeister 461 Posting Virtuoso

Got the idea from Vegaseat's StringBuilder experiment.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.