Richbox或TXTBox未显示UTF-8字符



我正在制作一个程序,将文件的所有字节转换为纯文本(在UTF-8中编码),当我想在Richbox或一个文本框,它仅显示/写几个字符或没有字符。当文件仅包含Unicode简单字符(拉丁字母)和数字

时,此问题不会出现。

这是我的代码:

richTextBox1.Text = System.Text.Encoding.UTF8.GetString(File.ReadAllBytes(FilePath));

为什么会发生这种情况?,这是VB中的错误还是我的错?任何类型的帮助都将不胜感激

您可能想尝试我当前正在使用的C#解决方案:

byte[] bytes = { 130, 200, 234, 23 }; // A byte array contains non-ASCII (or non-readable) characters
string s1 = Encoding.UTF8.GetString(bytes); // ���
byte[] decBytes1 = Encoding.UTF8.GetBytes(s1);  // decBytes1.Length == 10 !!
// decBytes1 not same as bytes
// Using UTF-8 or other Encoding object will get similar results
string s2 = BitConverter.ToString(bytes);   // 82-C8-EA-17
String[] tempAry = s2.Split('-');
byte[] decBytes2 = new byte[tempAry.Length];
for (int i = 0; i < tempAry.Length; i++)
    decBytes2[i] = Convert.ToByte(tempAry[i], 16);
// decBytes2 same as bytes
string s3 = Convert.ToBase64String(bytes);  // gsjqFw==
byte[] decByte3 = Convert.FromBase64String(s3);
// decByte3 same as bytes
string s4 = HttpServerUtility.UrlTokenEncode(bytes);    // gsjqFw2
byte[] decBytes4 = HttpServerUtility.UrlTokenDecode(s4);
// decBytes4 same as bytes

只需将语法调整为vb.net,它很简单...

最新更新