c# - 如何将 unicode UCS-2 格式的文本转换为 C# 可读?

标签 c# unicode ucs2

我在 SIM900 GPRS 中通过短信收到此消息。

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

提前致谢。

最佳答案

在维基百科上阅读我发现了这个 article , 表示 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℈ɰ䔂舁j密码不正确。 P٬ကճ湐؄灐ठ田弗怀斯德怀ɠ怀Ç退

关于c# - 如何将 unicode UCS-2 格式的文本转换为 C# 可读?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11583256/

相关文章:

python - Unicode 字符无法在终端 python 中正确打印

python - 我需要将 € 欧元符号转换为 IKw=,它应该是 base64 编码

c# - 单击列标题时,如何对 DataGridView 中的 DataBound 列进行排序?

c# - 我真的需要整个 Grpc.Core NuGet 包才能在简单的 C# 应用程序中使用 Google PubSub

c# - 如何通过代码配置 ASP.Net 成员资格提供程序?

delphi - 将 Char 转换为 AnsiChar 或 WideChar (Delphi)

delphi - 使用 Delphi 7 进行开发时,准备好使用 Delphi 2009 及更高版本了吗?

c# - .Net async ForEach() 委托(delegate)抛出任务取消异常

utf-8 - 将 SQL Server 2008 R2 的编码(排序规则?)更改为 UTF-8

c# - 关于字符类,哪个 .NET 平台和哪个版本的 Windows 支持哪个版本的 Unicode?