Text to Binary Conversion using C#

In this tutorial, I will teach you how to create a program in C# that converts an inputted text to a binary. Because we all know that computers store all characters as numbers stored as binary data. Binary code uses the digits of 0 and 1 (binary numbers) to represent computer instructions or text. So, now let's start this tutorial! 1. Let's start with creating a Windows Form Application in C# for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application and name your program ConvertTextToBinary. 2. Next, add two TextBox named TextBox1 to input a text and TextBox2 for displaying the binary conversion of the text you have inputted. Add also a button named Button1. Design your interface like this one below: design 3. Put this code to your Button1_Click. This will display the conversion of the text to binary. Initialize Temp as String to nothing, and txtBuilder to instantiate into a StringBuilder. System.Text.StringBuilder represents a string-like object whose value is a mutable sequence of characters. The value is said to be mutable because it can be modified once it has been created by appending, removing, replacing, or inserting characters.
  1. string Temp = "";
  2. System.Text.StringBuilder txtBuilder = new System.Text.StringBuilder();
Create a for each loop with a variable Character as byte to get the equivalent bytes of the TextBox1 using System.Text.ASCIIEncoding.ASCII.GetBytes. The txtBuilder variable will be appended after the conversion of the Character Bytes to String. The padleft method below returns a new string of a specified length in which the beginning of the current string is padded with spaces or with a specified Unicode character. And this padleft right -a ligns the characters. We have also appended a space for us to separate each character as they have different binary equivalents.
  1. foreach (byte Character in System.Text.ASCIIEncoding.ASCII.GetBytes(TextBox1.Text))
  2. {
  3. txtBuilder.Append(Convert.ToString(Character, 2).PadLeft(8, '0'));
  4. txtBuilder.Append(" ");
  5. }
After the For Each Loop, the Temp variable will have a value of the TextBuilder with its specified length. Next, all the conversion process holds the Temp Variable and it will be equal and displayed in TextBox2.
  1. Temp = txtBuilder.ToString().Substring(0, txtBuilder.ToString().Length - 0);
  2. TextBox2.Text = Temp;
Full source code:
  1. using System.Diagnostics;
  2. using System;
  3. using System.Windows.Forms;
  4. using System.Collections;
  5. using System.Drawing;
  6. using System.Data;
  7. using System.Collections.Generic;
  8.  
  9.  
  10.  
  11. namespace ConvertTextToBinary
  12. {
  13.         public partial class Form1
  14.         {
  15.                 public Form1()
  16.                 {
  17.                         InitializeComponent();
  18.                 }
  19.                
  20.                
  21.                 public void Button1_Click(System.Object sender, System.EventArgs e)
  22.                 {
  23.                         string Temp = "";
  24.                         System.Text.StringBuilder txtBuilder = new System.Text.StringBuilder();
  25.                         foreach (byte Character in System.Text.ASCIIEncoding.ASCII.GetBytes(TextBox1.Text))
  26.                         {
  27.                                 txtBuilder.Append(Convert.ToString(Character, 2).PadLeft(8, '0'));
  28.                                 txtBuilder.Append(" ");
  29.                         }
  30.                         Temp = txtBuilder.ToString().Substring(0, txtBuilder.ToString().Length - 0);
  31.                         TextBox2.Text = Temp;
  32.                 }
  33.         }
  34.        
  35. }
Output: output Download the source code below and try it! :) Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment