Binary To Text Conversion using C#

Today, i will teach you how to create a program that converts binary into text using C#. This is a continuation of my other tutorial in C# entitled Text to Binary Conversion using C#. 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. Name the program Convert Binary to text. 2. Next, add two TextBox named TextBox1 to input a binary digit and TextBox2 for displaying the text conversion of the binary you have inputted. Add also a button named Button1. Design your layout like this one below: design 3. Put this code to your Button1_Click. This will display the conversion of the binary to text. Initialize tempString as String to nothing and a Character as String that holds a regular expression of the textbox1 to 1 or 0 only as our System.Text.RegularExpressions.Regex as we use the Replace method.
  1. string tempString = "";
  2. string Character = System.Text.RegularExpressions.Regex.Replace(TextBox1.Text, "[^01]", "");
Next, we declare a Bytes variable that have the length of 8 bits as we have the code ((Character.Length / 8) - 1).
  1. byte[] Bytes = new byte[(Character.Length / 8) - 1 + 1];
Then we will have a For Next loop that we initialized an Index variable as an integer to start from 0 to the Bytes variable length minus 1 because it is an array of bytes (Note: array starts at 0 so we minus 1 it). Next, the Bytes array index will be equal to the conversion of bytes from the characters inputted in textbox1.
  1. for (int Index = 0; Index <= Bytes.Length - 1; Index++)
  2. {
  3. Bytes[Index] = Convert.ToByte(Character.Substring(Index * 8, 8), 2);
  4. }
The tempString variable will get the equivalent string of the variable Bytes. And then the value of the binary inputted will now be equal to the variable tempString and will be displayed in textbox2.
  1. tempString = (string) (System.Text.ASCIIEncoding.ASCII.GetString(Bytes));
  2. TextBox2.Text = tempString;
  3. }
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 Convert_Binary_to_text
  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 tempString = "";
  24. string Character = System.Text.RegularExpressions.Regex.Replace(TextBox1.Text, "[^01]", "");
  25. byte[] Bytes = new byte[(Character.Length / 8) - 1 + 1];
  26. for (int Index = 0; Index <= Bytes.Length - 1; Index++)
  27. {
  28. Bytes[Index] = Convert.ToByte(Character.Substring(Index * 8, 8), 2);
  29. }
  30. tempString = (string) (System.Text.ASCIIEncoding.ASCII.GetString(Bytes));
  31. TextBox2.Text = tempString;
  32. }
  33. }
  34.  
  35. }
Output: output Download the code 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

Comments

Add new comment