8

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.

KF2
  • 9,887
  • 8
  • 44
  • 77
Fusionmate
  • 679
  • 1
  • 8
  • 18
  • 4
    Do you *have* to start with a `short` array? If you could start with a `char` array instead (each `char` is a 16-bit unsigned integer) then it would be a lot simpler... – Jon Skeet Apr 04 '13 at 15:21

5 Answers5

10

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);
7

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);
Buh Buh
  • 7,443
  • 1
  • 34
  • 61
3

Try this:

//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);
Leo Chapiro
  • 13,678
  • 8
  • 61
  • 92
  • This isn't converting a short[] to a string... it's _re-writing the code_ for the short[] as char[] and then converting _that_ to a string. – Sepster Apr 04 '13 at 15:22
  • 1
    @Sepster: The asker wants a string, while they said they need a short[], they may not realize that char supports UTF-16. – Guvante Apr 04 '13 at 15:24
  • 1
    @Guvante "Is it possible to convert short array to string" is the question. Not disagreeing that the answer is useful... (which is why I haven't downvoted), but as it sits, it doesn't answer _the question_. – Sepster Apr 04 '13 at 15:25
  • @Sepster: But it does solve Fusionmate's problem if he doesn't have a real reason to use `short`... – Guvante Apr 04 '13 at 15:28
  • 2
    @Guvante I don't disagree. _If_ he doesn't have a real reason to use `short`. :-) Just suggesting the answer at least needed some qualification to that effect. – Sepster Apr 04 '13 at 15:30
1

You need an array of char. string has an overload that accepts one directly.

char[] temp = new char[a.Length];
Array.Copy(a, temp, a.Length);
return new string(temp);

Unfortunately this involves copying the entire array. You could theoretically avoid this by using some casting tricks and unsafe code, but it would be tricky.

Ideally as others have mentioned, you would start with a char[] instead of a short[]. For instance if you are loading from a file, you could store the numbers you find into a char[] after casting them.

Guvante
  • 18,775
  • 1
  • 33
  • 64
0

try this:

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);
KF2
  • 9,887
  • 8
  • 44
  • 77