我有一个字符串表示为一个字节数组,我知道该数组已经删除了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字符?
发布于 2017-12-23 07:50:24
前256个代码点与ISO-8859-1的内容相同,从而使转换现有的西方文本变得琐碎。
https://en.m.wikipedia.org/wiki/Unicode
Encoding.GetEncoding("ISO-8859-1").GetString(bytes)
发布于 2017-12-23 06:48:31
如果您知道所有字节都是0x7f
或更少,则可以将它们视为utf-8
并使用System.Text.UTF8Encoding
转换器类。
https://stackoverflow.com/questions/47950368
复制相似问题