1

Here is my problem: I have a string that I think it is binary:

zv�Q6��.�����E3r

I want to convert this string to something which can be read. How I can do this in C#?

2
  • 3
    You need to know what encoding to use. Commented Aug 27, 2013 at 9:41
  • I remember that I used bin2hex function in PHP for this case and got proper convert. Does it mean that I should convert this string to hex in C#? Commented Aug 27, 2013 at 9:44

2 Answers 2

1

You may try enumerating (testing) all available encodings and find out that one which encodes reasonable text. Unfortunately, when it's not an absolute solution: it could be a information loss on erroneous conversion.

   public static String GetAllEncodings(String value) {
      List<Encoding> encodings = new List<Encoding>();

      // Ordinary code pages
      foreach (EncodingInfo info in Encoding.GetEncodings()) 
        encodings.Add(Encoding.GetEncoding(info.CodePage));

      // Special encodings, that could have no code page
      foreach (PropertyInfo pi in typeof(Encoding).GetProperties(BindingFlags.Static | BindingFlags.Public))
        if (pi.CanRead && pi.PropertyType == typeof(Encoding))
          encodings.Add(pi.GetValue(null) as Encoding);

      foreach (Encoding encoding in encodings) {
        Byte[] data = Encoding.UTF8.GetBytes(value);
        String test = encoding.GetString(data).Replace('\0', '?');

        if (Sb.Length > 0)
          Sb.AppendLine();    

        Sb.Append(encoding.WebName);
        Sb.Append(" (code page = ");
        Sb.Append(encoding.CodePage);
        Sb.Append(")");

        Sb.Append(" -> ");
        Sb.Append(test);
      }

      return Sb.ToString();
    }

    ...


// Test / usage

    String St = "Некий русский текст";      // <- Some Russian Text
    Byte[] d = Encoding.UTF32.GetBytes(St); // <- Was encoded as UTF 32
    St = Encoding.UTF8.GetString(d);        // <- And erroneously read as UTF 8

    // Let's see all the encodings:
    myTextBox.Text = GetAllEncodings(St);

    // In the myTextBox.Text you can find the solution:
    // ....
    // utf-32 (code page = 12000) -> Некий русский текст
    // ....
Sign up to request clarification or add additional context in comments.

Comments

1
byte[] hexbytes = System.Text.Encoding.Unicode.GetBytes();

this gives you hex bytes of the string but you have to know the encoding of your string and replace the 'Unicode' with that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.