2

i'm just started dev-ing app for wp7 and I'm trying to convert a string of binary back to ascii using c#.

But i have no idea how can it be done. Hope someone could help me out here.

example :

Input string: 0110100001100101011011000110110001101111

Output string: hello

ctacke
  • 66,480
  • 18
  • 94
  • 155
JustStarted
  • 199
  • 4
  • 13
  • 1
    Possible duplicate: http://stackoverflow.com/questions/6006425/binary-to-corresponding-ascii-string-conversion – Gloopy Jul 01 '12 at 08:42

2 Answers2

3

The simple solution,

using SubString and built in Convert.ToByte could look like this:

string input = "0110100001100101011011000110110001101111";
int charCount = input.Length / 8;
var bytes = from idx in Enumerable.Range(0, charCount)
            let str = input.Substring(idx*8,8)
            select Convert.ToByte(str,2);
string result = Encoding.ASCII.GetString(bytes.ToArray());
Console.WriteLine(result);

Another solution, doing the calculations yourself:

I added this in case you wanted to know how the calculations should be performed, rather than which method in the framework does it for you:

string input = "0110100001100101011011000110110001101111";
var chars = input.Select((ch,idx) => new { ch, idx});
var parts = from x in chars
            group x by x.idx / 8 into g
            select g.Select(x => x.ch).ToArray();

var bytes = parts.Select(BitCharsToByte).ToArray();
Console.WriteLine(Encoding.ASCII.GetString(bytes));

Where BitCharsToByte does the conversion from a char[] to the corresponding byte:

byte BitCharsToByte(char[] bits) 
{
    int result = 0;
    int m = 1;
    for(int i = bits.Length - 1 ; i >= 0 ; i--) 
    {
        result += m * (bits[i] - '0');
        m*=2;
    }
    return (byte)result;
}

Both the above solutions does basically the same thing: First group the characters in groups of 8; then take that sub string, get the bits represented and calculate the byte value. Then use the ASCII Encoding to convert those bytes to a string.

driis
  • 161,458
  • 45
  • 265
  • 341
  • Hi driis, Thanks for your reply, I've tried the method you mentioned above. However, Encoding.ASCII isn't supported in wp7 and it is also the reason which makes me stucked for a very long time. – JustStarted Jul 01 '12 at 12:23
0

You can use the BitArray Class and use its CopyTo function to copy ur bit string to byte array

Then you can convert your byte array to string using Text.Encoding.UTF8.GetString(Byte[])

See this link on BitArray on MSDN

Uday0119
  • 770
  • 1
  • 7
  • 23
  • 1
    Doing this directly would flip the order of the bits in the resulting `byte[]`, so the code would have to take that into account. – driis Jul 01 '12 at 09:12
  • Hi Uday0119, Thanks for your reply too. Yes! it seems that you will have to use `Text.Encoding.UTF8.GetString(Byte[])` in wp7 instead of `Text.Encoding.ASCII.GetString(Byte[])` . – JustStarted Jul 01 '12 at 12:27
  • Oh, and for those who received this error message "System.Text.encoding.GetString(byte[]) is inaccessible due to protection level", you might want to take a look at this [link](http://forums.silverlight.net/t/159871.aspx/1) – JustStarted Jul 01 '12 at 12:29
  • Hi @juststarted, so i hope you get ur resolution. If you feel this as answer to ur query. Then please mark this as accepted. Thanx – Uday0119 Jul 05 '12 at 03:02