-3
string GetBits(string input)
{
    StringBuilder sb = new StringBuilder();            
    foreach (byte b in Encoding.Unicode.GetBytes(input))
    {
        sb.Append(Convert.ToString(b, 2));
    }
    return sb.ToString();
}

I found this function online to convert a string into binary. How would I do the reverse?

For example converting the word "hello" to binary using the function given above and then converting the resulting binarystring back into the original word/string?

I'm trying to do this in C# and that my input which I want to convert back is the string given by the function above

5
  • This is pretty much your answer C# Byte array to Unicode string
    – Nathan
    Commented Apr 27, 2016 at 11:12
  • I already tried this, but somehow it only gives out gibberish. Could it be because of the format provider when converting the byte to string? Commented Apr 27, 2016 at 11:15
  • 1
    You should rather use bold text **text** instead of going for uppercase. Makes it looking bad.
    – C4d
    Commented Apr 27, 2016 at 11:24
  • If you do not have unicode characters don't use unicode encoding. Instead use Encoding.UTF8. Strings/characters are two byte objects with a private property that indicates Why use a binary string when you can use a byte array. Use Encoding.UTF8.GetBytes(string) and then Encoding.UTF8.GetString(byte[]);
    – jdweng
    Commented Apr 27, 2016 at 11:24
  • Thanks for your help. But please note that I'm using a string as input. I(f I convert it to a bytearray and then back to a string it won't help me. I bgasically need to do the reverse of what the function given in my post does. Commented Apr 27, 2016 at 11:33

1 Answer 1

1

The main problem is that your input function is wrong.
If you pass the string "Hello" the returned string is

 "10010001100101110110011011001101111" (35 characters ??? )

but this is not a correct representation in binary of the word Hello where every character should be represented by a series of 8 0|1 (total 40 characters)

The main reason for this error lies in the Convert.ToString that strips away the leading zeros from the byte passed to the conversion.
So the letter H should be 01001000 (8 chars) but the conversion returns 1001000 (7 chars) thus everything that follow is wrong and impossible to correctly convert back.

You could fix the input function with

string GetBits(string input)
{
    StringBuilder sb = new StringBuilder();

    foreach (byte b in Encoding.UTF8.GetBytes(input))
    {
        string temp = Convert.ToString(b, 2);

        // This ensure that 8 chars represent the 8bits
        temp = "00000000".Substring(temp.Length) + temp; 
        sb.Append(temp);
    }
    return sb.ToString();
}

While the conversion back to "Hello" could be written as

string GetString(string input)
{
    // Probably a checking on the input length as multiple of 8 
    // should be added here....

    StringBuilder sb = new StringBuilder();
    while (input.Length > 0)
    {
        string block = input.Substring(0, 8);
        input = input.Substring(8);
        int value = 0;
        foreach (int x in block)
        {
            int temp = x - 48;
            value = (value << 1) | temp;
        }
        sb.Append(Convert.ToChar(value));
    }
    return sb.ToString();
}
2
  • thank you so much! What would I have to change when using unicode? Just Encoding.Unicode.GetBytes(input) Commented Apr 27, 2016 at 12:11
  • Uhm not only that. Also the conversion back to string requires changes to handle blocks of 16 bytes. But this should be tested and I am sorry but I have no time now
    – Steve
    Commented Apr 27, 2016 at 12:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.