Is it possible to convert short
array to string
, then show the text?
short[] a = new short[] {0x33, 0x65, 0x66, 0xE62, 0xE63};
There are utf16
(thai characters) contains in the array. How can it output and show the thai and english words?
Thank you.
You can get a string from a UTF16 byte array using this method:
System.Text.Encoding.Unicode.GetString(bytes)
However, this only accepts an byte array. So you first have to transform your shorts to bytes:
var bytes = a.SelectMany(x => BitConverter.GetBytes(x)).ToArray();
Or slightly more verbose but much more efficient code:
var bytes = new byte[a.Length * 2];
Buffer.BlockCopy(a, 0, bytes, 0, a.Length * 2);
I'm slightly ripping off everyone else's answers, but here is a cleaner way of doing the same thing:
short[] shorts = new short[] { 0x33, 0x65, 0x66, 0xE62, 0xE63 };
char[] chars = Array.ConvertAll(shorts, Convert.ToChar);
string result = new string(chars);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With