How to convert text in unicode UCS-2 format to readable with C#?

embarus picture embarus · Jul 20, 2012 · Viewed 10.4k times · Source

I get this message in an SMS in SIM900 GPRS.

07916698019021F00410D05479BDDC7CBBCB790008217002123430826A0049006E0063006F00720072006500630074002000700061007300730077006F00720064002E00200050006C050610306500065060740507202079060750702007001070730700F0700402001060010600E02

And another sample message:

07916698019021F00410D05479BDDC7CBBCB790008217002025501826A0049006E0063006F00720072006500630074002000700061007300730077006F00720064002E00200050006C06001073050200506E04065070200906F07072020700607307007060020600006007060090600E

I think this message is in Unicode UCS-2 format and is in Thai. However I can't convert it to something readable. I found this very useful code:

//Here's how you'd go from a string to stuff like
// U+0053 U+0063 U+006f
string scott = "ฉ";
foreach (char s in scott) {
  Console.Write("{0:x4} ", (int)s);
}
//Here's how converted a string (assuming it starts with U+)
// containing the representation of a char
// back to a char
// Is there a built in, or cleaner way? Would this work in Chinese?
string maybeC = "U+0063";
int p = int.Parse(maybeC.Substring(2),
 System.Globalization.NumberStyles.HexNumber);
Console.WriteLine((char)p);

Thanks in advance.

Answer

L.B picture L.B · Jul 20, 2012

Reading on Wikipedia I found this article, says that UCS-2 is very similiar to UTF-16. So:

string s = "07916698019021F00410D05479BDDC7CBBCB790008217002025501826A0049006E0063006F00720072006500630074002000700061007300730077006F00720064002E00200050006C06001073050200506E04065070200906F07072020700607307007060020600006007060090600E";
List<byte> bytes = new List<byte>();
for (int i = 0; i < s.Length; i+=2)
{
    bytes.Add(byte.Parse(s.Substring(i, 2), NumberStyles.HexNumber));
}

var str = Encoding.Unicode.GetString(bytes.ToArray());

OUTPUT: 鄇顦送င哐뵹糜쮻y℈ɰ唂舁jIncorrect password. P٬ကճ湐؄灐ठ牰܂怀ݳ瀀ɠ怀؇退