Hi,

I need to convert a string of binary numbers to their corresponding ASCII characters.
E.g. 011000010110001001100011 should be displayed as ‘abc’.
I tried couple of functions in C# but could not get the desired result.

To get a more clear idea of what I need, please visit

http://www.theskull.com/javascript/ascii-binary.html

Enter in the second text box the value 011000010110001001100011
Clicking convert will display the text ‘abc’

Any ideas how to get this in a C# code?

One idea might be to take first 8 digits, convert it to it’s corresponding character and enter in an empty string. Then, select next 8 digits,convert and append to the previous string. But not much sure how to achieve this.

Regards

Look I'm still working but to convert from String to Byte you you need first to convert it to Binary then to Byte (There's may better solution...)

ASCIIEncoding ascii = new ASCIIEncoding();
            byte[] bytes = ascii.GetBytes("abc");
            string result = "";
            foreach (byte b in bytes)
                result  += Convert.ToString(b, 2);
            MessageBox.Show(result );

I'm working right now in the reverse.

The reverse

List<byte> byteForChar = new List<byte>();
            string input = "abc";
            string result = "";
            for (int i = 0; i < input.Length; i++)
                byteForChar.Add(Convert.ToByte(input[i]));

            foreach (byte b in byteForChar.ToArray())
                for (int i = 0; i <= 7; i++)
                {
                    if ((b & (int)Math.Pow(2, i)) != 0)
                        result += "1";
                    else
                        result += "0";
                    }
            MessageBox.Show(result);

Reference | http://www.neowin.net/forum/index.php?showtopic=625604

private void btnEncode_Click(object sender, EventArgs e)
{
  if (!string.IsNullOrEmpty(txtInFile.Text))
  {
    FileStream fs = new FileStream(txtInFile.Text, 
                                   FileMode.Open, 
                                   FileAccess.Read);
    byte[] filebytes = new byte[fs.Length];
    fs.Read(filebytes, 0, Convert.ToInt32(fs.Length));
    string encodedData = 
        Convert.ToBase64String(filebytes,                 
                               Base64FormattingOptions.InsertLineBreaks);
    txtEncoded.Text = encodedData; 
  }
}

private void btnDecode_Click(object sender, EventArgs e)
{
  if (!string.IsNullOrEmpty(txtOutFile.Text))
  {
    byte[] filebytes = Convert.FromBase64String(txtEncoded.Text);
    FileStream fs = new FileStream(txtOutFile.Text, 
                                   FileMode.CreateNew, 
                                   FileAccess.Write, 
                                   FileShare.None);
    fs.Write(filebytes, 0, filebytes.Length);
    fs.Close(); 
  }
}
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.