• We just launched and are currently in beta. Join us as we build and grow the community.

Text to Binary Conversion using C#

milesp

Digital Search Specialist
M Rep
0
0
0
Rep
0
M Vouches
0
0
0
Vouches
0
Posts
116
Likes
191
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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:

textdesign.png


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. namespace

    ConvertTextToBinary
  9. {
  10. public

    partial

    class

    Form1
  11. {
  12. public

    Form1(

    )
  13. {
  14. InitializeComponent(

    )

    ;
  15. }


  16. public

    void

    Button1_Click(

    System

    .

    Object

    sender, System

    .

    EventArgs

    e)
  17. {
  18. string

    Temp =

    ""

    ;
  19. System.Text

    .

    StringBuilder

    txtBuilder =

    new

    System.Text

    .

    StringBuilder

    (

    )

    ;
  20. foreach

    (

    byte

    Character in

    System.Text

    .

    ASCIIEncoding

    .

    ASCII

    .

    GetBytes

    (

    TextBox1.

    Text

    )

    )
  21. {
  22. txtBuilder.

    Append

    (

    Convert.

    ToString

    (

    Character, 2

    )

    .

    PadLeft

    (

    8

    , '0'

    )

    )

    ;
  23. txtBuilder.

    Append

    (

    " "

    )

    ;
  24. }
  25. Temp =

    txtBuilder.

    ToString

    (

    )

    .

    Substring

    (

    0

    , txtBuilder.

    ToString

    (

    )

    .

    Length

    -

    0

    )

    ;
  26. TextBox2.

    Text

    =

    Temp;
  27. }
  28. }

  29. }


Output:

textoutput_0.png


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


Download
You must upgrade your account or reply in the thread to view hidden text.
 

450,270

322,965

322,974

Top