如何将unicode转义序列转换为十六进制格式。我从串口获取zigbee数据。 [英] How can I convert a unicode escape sequence to hexadecimal form.i m getting data though zigbee from serial port.
问题描述
我从下面给出的表格中获取串口数据。
〜\\\\ u0016?\0\\\?\ 0AT ?? \\? \ u0001#yfgyhvkh#?
和XCTU软件中的数据相同。
7E 00 15 90 00 13 A2 00 41 54 EC 8E 5C 9F 01 23 31 32 23 31 32 23 52 23 0B
任何人都可以给我一些代码或算法如何转换收到的字符串到了hexa。
我尝试了很多方法,但没有转换。请提前告诉你。
我有什么试过:
string unicode =\0\\\?\\\\ u0013?\ 0AT ?? \\? \ u0001#yfgyhvkh#;
string str = System.Uri.UnescapeDataString(unicode);
简单:不要从串口读取它作为字符串。
使用的SerialPort .Read方法(字节[],Int32,Int32)(System.IO.Ports) [ ^ ]并且设备中的每个单独值将作为单独的字节值到达,不会转换为Unicode。
如果需要,可以使用 [ ^ ]
SerialPort
component提供了读取字节数组而不是字符串的方法。你应该使用它们。然后,用十六进制字符串表示字节数组是微不足道的(例如:参见: c# - 如何将字节数组转换为十六进制字符串,反之亦然? - 堆栈溢出 [ ^ ])。
I am Getting data from serial port in the form given below.
"~\0\u0016?\0\u0013?\0AT??\\?\u0001#yfgyhvkh#?"
And same data in the XCTU software i am getting as .
7E 00 15 90 00 13 A2 00 41 54 EC 8E 5C 9F 01 23 31 32 23 31 32 23 52 23 0B
Can anyone please give me some code or algorithm how to convert that received string to hexa.
I have tried many methods but its not converting.Thank you so much in advance.
What I have tried:
string unicode = "\0\u0016?\0\u0013?\0AT??\\?\u0001#yfgyhvkh#";
string str = System.Uri.UnescapeDataString(unicode);
Simple: don't read it from the serial port as a string.
Read it as bytes using SerialPort.Read Method (Byte[], Int32, Int32) (System.IO.Ports)[^] and each separate value from the device will arrivae as a separate byte value with no translation to Unicode.
If you need to, you can convert the byte array to a hex string using BitConverter.ToString[^]
The SerialPort
component provides methods for reading byte arrays instead of strings. You should use them. Then, representing array of bytes with hexadecimal strings is trivial (see, for instance: c# - How do you convert a byte array to a hexadecimal string, and vice versa? - Stack Overflow[^]).
这篇关于如何将unicode转义序列转换为十六进制格式。我从串口获取zigbee数据。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!