0

I have a string array that contains some binary data, and I would like to convert the binary to its equivalent character representative. Each element inside the array contains 8 bit "1 byte" of data and I need to know how to convert it to its character equivalence

Here is the string array:

IEnumerable<string> resultChunks = Enumerable.Range(0, result.Length / 8)
   .Select(x => result.Substring(x * 8, 8));
            string[] newRes = resultChunks.ToArray();
            string tempRes="";

            for (int i = 0; i < newRes.Length; i++)
            {
                tempRes+=Convert.ToString(newRes[i]);

            }

Current "result" is "0010001111000100001010010011101111000111001100110110011100110110"

WT86
  • 823
  • 5
  • 13
  • 34
  • 1
    Do you have an example? What did you try? – Chief Wiggum Nov 05 '14 at 09:40
  • can you please paste the string.. for sample to verify its working – Deepak Sharma Nov 05 '14 at 09:40
  • what do you mean by binary data? "0010001111000100001010010011101111000111001100110110011100110110" or "♂453§◘AA§A" ?? – Hamid Pourjam Nov 05 '14 at 10:03
  • define string **array** contains some **binary data** please – Hamid Pourjam Nov 05 '14 at 10:05
  • Please provide a good code example and more details. You need to explain exactly what the input looks like, with examples, as well as exactly what output you expect, and why that's different from the output you're getting now. See http://stackoverflow.com/help/mcve for help on how to write a good code example. – Peter Duniho Nov 05 '14 at 18:06

2 Answers2

0

If your data is in a byte array then you could use this:

string result = Encoding.ASCII.GetString(bytes);
Sjips
  • 1,248
  • 2
  • 11
  • 22
0

I guess, your string is base64 string. Then you can use next method:

public static string FromBase64(string base64Str, Encoding encoding = null)
{
        if (string.IsNullOrWhiteSpace(base64Str))
        {
            return string.Empty;
        }
        byte[] bytes;
        try
        {
            bytes = System.Convert.FromBase64String(base64Str);
        }
        catch (FormatException)
        {
            return string.Empty;
        }
        var stringEncoding = encoding ?? Encoding.UTF8;
        return stringEncoding.GetString(bytes);
    }
Artem Kachanovskyi
  • 1,839
  • 2
  • 18
  • 27