3

我在 SIM900 GPRS 的 SMS 中收到此消息。

07916698019021F00410D05479BDDC7CBBCB790008217002123430826A0049006E0063006F00720072006500630074002000700061007300730077006F00720064002E00200050006C050610306500065060740507202079060750702007001070730700F0700402001060010600E02

另一个示例消息:

07916698019021F00410D05479BDDC7CBBCB790008217002025501826A0049006E0063006F00720072006500630074002000700061007300730077006F00720064002E00200050006C06001073050200506E04065070200906F07072020700607307007060020600006007060090600E

我认为这条消息是 Unicode UCS-2 格式并且是泰语。但是我无法将其转换为可读的东西。我发现这个非常有用的代码:

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

提前致谢。

4

2 回答 2

4

在 Wikipedia 上阅读我发现这篇文章,说 UCS-2 与 UTF-16 非常相似。所以:

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

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

于 2012-07-20T16:44:15.427 回答
0

尝试使用内置System.Text.Encoding类。

using System.Text;
// ..
var bytes = Encoding.GetEncoding("ucs-2").GetBytes("SomeString");

编辑:您可以从 UCS-2/UTF-16 编码转换为GetString(byte).

于 2012-07-20T16:26:03.297 回答