为什么我在UCS2中得到的数字如何修复命令和c#?
我在通过putty读取我的短信时遇到问题,因为我键入AT + CMGL =“ALL”但是消息(文本)和数字只是数字,我读到我的gms调制解调器nokia s10使用UCS2,但我不知道该怎么办? 我怎样才能看到我刚刚看到数字的消息? 请帮忙
我也在使用codeproject中的这段代码而且我更改了这一行但是它和upt2中的putty一样是相同的结果
public ShortMessageCollection ReadSMS(SerialPort port, string p_strCommand) { // Set up the phone and read the messages ShortMessageCollection messages = null; try { #region Execute Command // Check connection ExecCommand(port,"AT", 300, "No phone connected"); // Use message format "Text mode" ExecCommand(port,"AT+CMGF=1", 300, "Failed to set message format."); // Use character set "PCCP437" **ExecCommand(port, "AT+CSCS="UCS2"", 300, "Failed to set character set.")**; // Select SIM storage ExecCommand(port,"AT+CPMS="SM"", 300, "Failed to select message storage."); // Read the messages string input = ExecCommand(port, p_strCommand, 5000, "Failed to read the messages."); #endregion #region Parse messages messages = ParseMessages(input); #endregion } catch (Exception ex) { throw ex; } if (messages != null) return messages; else return null; }
请注意, AT+CSCS
仅影响命令和响应的字符串参数。 在AT+CMGL
的情况下,消息的内容不是字符串,而是格式。 有关该格式的更多详细信息,请参阅27.005规范,它有点复杂(仅注意第一个
In the case of SMS
部分In the case of SMS
,忽略第二个In the case of CBS
部分In the case of CBS
)。
但它的简短版本是,对于UCS-2,您将获得hex编码的数据(例如,两个字符'2'
和'A'
表示一个字节,值为0x2A
(ASCII / UTF-8字符'*'
))。 因此,您应该将4和4个接收字节解码为UCS-2字符中16位的hex编码。
因此解码成字节数组然后转换为字符串,请参阅Appleman1234的答案 (他的答案没有解决核心问题,即hex解码)。
要从UCS-2编码转换,将结果(输入)存储在字节数组中而不是字符串然后调用
System.Text.Encoding enc = Encoding.Unicode; string myString = enc.GetString(myByteArray);
如果UCS-2编码是Big Endian,则更改System.Text.Encoding enc = Encoding.Unicode;
to System.Text.Encoding enc = Encoding.BigEndianUnicode;
。
相关资源包括:
上述就是C#学习教程:为什么我在UCS2中得到的数字如何修复命令和c#?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1296332.html