How Do I Convert Binary To ASCII

Hello Everyone,

I have a problem that I hope someone can help me with. I'm just learning Visual C# .NET and trying out different things in it. At the moment, I'm trying to make a program that converts text into Hexadecimal, Decimal, Octal and Binary form. That part works perfectly with the little code I have inserted below. This little code converts my text into binary form:

[code]
[color=Blue]char[/color][] arrChars = txtASCII.Text.ToCharArray();
String sBinary = "";
String D;
D = " ";
[color=Blue]foreach[/color] ([color=Blue]char[/color] ch [color=Blue]in[/color] arrChars)
{
sBinary += Convert.ToString(Convert.ToInt32(ch),2) + D;

}
txtBIN.Text = sBinary;
[/code]
How would I now reverse this? If I want to read the binary box instead of the text box and convert it back to text, since the binary values varies in length. As an example, space is 100000 and Alt + 0256 displays 100000001.

I guess I can make "If" statements up to my "space" separator, and compare it with "0", "1", "10", "11", and so on and then type out it's text value, but there has to be a smarter, more effecient way to do that. Anyone who understands what I mean and can possibly help me? All help is appreciated since I'm just learning C#.

Thank You,
ScIeNcE_ErRoR

Comments

  • The inverse thing...

    [code]
    string[] arrStrings = this.txtBIN.Text.Trim(' ').Split(' ');
    String sText = "";
    foreach (string st in arrStrings)
    sText += Convert.ToChar(Convert.ToInt32(st, 2));
    this.txtASCII.Text = sText;
    [/code]


    [red]Good luck![/red]
    [blue]Hackman[/blue]
  • Thank you very, very much. This code example worked perfectly.

    Thank You,
    ScIeNcE_ErRoR
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories