是否可以将short
数组转换为string
,然后显示文本?
short[] a = new short[] {0x33, 0x65, 0x66, 0xE62, 0xE63};
数组中包含有泰文字符的utf16
编码。如何输出并显示泰文和英文单词?
谢谢。
System.Text.Encoding.Unicode.GetString(bytes)
然而,这个只接受字节数组。因此你必须先将你的 shorts 转换为 bytes:
var bytes = a.SelectMany(x => BitConverter.GetBytes(x)).ToArray();
或者稍微冗长但更高效的代码:
var bytes = new byte[a.Length * 2];
Buffer.BlockCopy(a, 0, bytes, 0, a.Length * 2);
short[] shorts = new short[] { 0x33, 0x65, 0x66, 0xE62, 0xE63 };
char[] chars = Array.ConvertAll(shorts, Convert.ToChar);
string result = new string(chars);
试试这个:
//short[] a = new short[] {0x33, 0x65, 0x66, 0xE62, 0xE63};
char[] a = new char[] {0x33, 0x65, 0x66, 0xE62, 0xE63};
string s = new string(a);
Console.WriteLine(s);
char
。 string
有一个直接接受一个字符数组的重载。char[] temp = new char[a.Length];
Array.Copy(a, temp, a.Length);
return new string(temp);
试一下这个:
short[] a = new short[] { 0x33, 0x65, 0x66, 0xE62, 0xE63 };
var contobyte = a.Select(b => (byte)b).ToArray();
var res = System.Text.Encoding.Unicode.GetString(contobyte);
short
数组开始?如果你可以用char
数组代替(每个char
是一个 16 位无符号整数),那么它会简单得多... - Jon Skeet