0

I am trying to convert binary numbers to ASCII characters. While I am displaying the binary numbers in the textbox or label, I am getting symbol "?". I want to get all ASCII characters include the extended ones with the original symbols.

I just couldn't get the extended ones. I am searching for two days and I couldn't figured out.

Can somebody help me ?

Edit : My code is this and when I am trying to extended ASCII's , I just get in the label "?"

Simple explanation :

String binary= "11001100";
label1.text = BinaryToString(binary);


public static string BinaryToString(string data)
    {
        List<Byte> byteList = new List<Byte>();

        for (int i = 0; i < data.Length; i += 8)
        {
            byteList.Add(Convert.ToByte(data.Substring(i, 8), 2));
        }

        return Encoding.ASCII.GetString(byteList.ToArray());
    }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 1
    Show what you have tried... – L.B Dec 13 '14 at 21:03
  • It sounds that you already have partially working code. You should show that so that it can be fixed, rather than asking for a solution from scratch. – Guffa Dec 13 '14 at 21:03

1 Answers1

0

Encoding.ASCII specifically says it only works on the lowest 128 characters. What you probably really want for arbitrary conversion of single bytes is code page 437, based on a related answer.

private static Encoding cp437 = Encoding.GetEncoding(437);
public static string BinaryToString(string data) {
    List<Byte> byteList = new List<Byte>();

    for (int i = 0; i < data.Length; i += 8) {
        byteList.Add(Convert.ToByte(data.Substring(i, 8), 2));
    }

    return cp437.GetString(byteList.ToArray());
}
Community
  • 1
  • 1
Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
  • Thanks for your answer Nathan , I tried but with your code it's gave me an error. The reason is it cannot convert byte 247 to unicode. Also when I tried to change the code "new UTF8Encoding(true, false);" it's not givin an error but I guess i'm avoiding some errors – Boğaçhan Gençtürk Dec 14 '14 at 00:56
  • Hmm. OK, I think I know what the deal is (namely that UTF-8 isn't designed to decode just any old set of bytes, but has some sequences that it won't encode for any defined character), so check my edit and see if it's right now. – Nathan Tuggy Dec 14 '14 at 02:41