1
A
回答
1
您可以嘗試枚舉(測試)所有可用的編碼,並找出編碼合理文本的 。不幸的是,當它不是一個絕對的解決方案: 它可能是一個錯誤轉換的信息損失。
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) -> Некий русский текст
// ....
1
byte[] hexbytes = System.Text.Encoding.Unicode.GetBytes();
這給你的字符串的十六進制字節,但你必須知道你的字符串的編碼,並用'Unicode'代替。
相關問題
- 1. 字符串轉換類型類類型
- 2. C宏將字符串轉換爲pascal字符串類型
- 3. (字符串)字符類型轉換
- 4. C#類型轉換(字符串轉換爲int)
- 5. 將字符串轉換爲字符串的泛型類型
- 6. C#字符串到浮點型轉換
- 7. 將LinqToExcel.RowNoHeader類型轉換爲字符串
- 8. 將MYSQL_ROW類型轉換爲字符串
- 9. 類型轉換爲unicode字符串?
- 10. 將字符串轉換爲'Date'類型
- 11. 通用類型轉換從字符串
- 12. 將字符串轉換爲類型「LuaFunction」
- 13. 字符串VB.Net類型轉換
- 14. 將字符串轉換爲類型UTCTime
- 15. 將字符串類型轉換爲int
- 16. JLayeredPane的類型轉換字符串
- 17. 類型轉換字符串和strdup
- 18. 將字符串值轉換爲類型
- 19. 轉換成字符串DateTime類型
- 20. 轉換字符串參數object類型
- 21. 類型轉換字符串INT
- 22. 無法將類型'int'隱式轉換爲'字符串'C#類
- 23. 將字符串類型轉換爲C中的8位數字#
- 24. C++字符串到字符串轉換
- 25. 隱式轉換的C++字符串類
- 26. 在C#字符串轉換到類名
- 27. JMSBytesMessage將字符串和字符串轉換爲JMSBytesMessage類型轉換
- 28. 類型字符串()的值不能轉換爲字符串
- 29. 字符串轉換爲美孚(字符串類型)
- 30. 無法將類型'字符串'隱式轉換爲'字符串'
您需要知道使用哪種編碼。 –
我記得我在這個例子中使用了PHP的bin2hex函數,並得到了適當的轉換。這是否意味着我應該在C#中將此字符串轉換爲十六進制? – A23149577