我的字符串表示为一个字符,我知道已删除了0x00
的高阶字节,因此该字符串被压缩为:
0x43 0x6F 0x6D 0x6D 0x61 0x6E 0x64 //"Command"
如何将字节转换为Unicode字符串?
我猜我需要将字节复制到一个新数组(uncompressedBytes
)的大小两倍,在每个第二个间隔:
byte[] compressedBytes = br.ReadBytes(stringLength);
byte[] uncompressedBytes = new byte[stringLength * 2];
for (int byteCounter = 0; byteCounter < stringLength; byteCounter++)
{
uncompressedBytes[byteCounter * 2] = compressedBytes[byteCounter];
}
return Encoding.Unicode.GetString(uncompressedBytes);
或是否有编码将所有字节视为缺少高阶字节的Unicode字符?
第一个256个代码点与ISO-8859-1的内容相同,以使转换现有的西方文本变得微不足道。
https://en.m.wikipedia.org/wiki/unicode
Encoding.GetEncoding("ISO-8859-1").GetString(bytes)
如果您知道所有字节是0x7f
或更少的字节,则可以将它们视为utf-8
并使用System.Text.UTF8Encoding
Converter类。