Reputation: 2145
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#?
Upvotes: 1
Views: 127
Reputation: 186668
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) -> Некий русский текст
// ....
Upvotes: 1
Reputation: 667
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.
Upvotes: 1