Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on a project,In which we are trying to transmitting the data from zigbee using API format.But,while getting data from serial port in software the receiving output is not same as the output getting in the xctu software.Can anyone help me in the decoding the received data.I tried to convert received data from ascii to both Big endian and small that didnot work for me.Please anyone can help me.I am sending data in frame where .Thank you so much in advance.

1.Receiving the data in ascii form like this.
"~\0\u0015?\0\u0013?\0AT??\\?\u0001#12#12#R#\v"
2.I want to get mac address which is 64bit in string from 6th to 13th byte.
3.Converting received data to hexa.
"7e00153f00133f0041543f3f5c3f012331322331322352230b"
4.Same data received in XCTU software.
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

What I have tried:

text = serialPort2.ReadExisting();
byte[] LogoDataBy = ASCIIEncoding.ASCII.GetBytes(text);
string wholeframe = ByteArrayToString(LogoDataBy);
var arr = LogoDataBy.Skip(5).Take(8).ToArray();

string macaddress = ByteArrayToString(arr);
Posted
Updated 2-Jan-18 19:50pm

1 solution

What you see that input string are Unicode escape sequences. So you have to un-escape them. Since we're dealing with byte arrays though and not wide characters (double byte arrays) we then convert them to ascii after the un-escaping to get the final result in a byte array. So, try something like this...
C#
static void Main(string[] args)
{
   // do not use the @ literal here
   var encodedString = "~\0\u0015?\0\u0013?\0AT??\\?\u0001#12#12#R#\v";

   // create two different encodings
   var ascii = Encoding.ASCII;
   var unicode = Encoding.Unicode;

   // asciiBytes will have your MAC address in decimal
   byte[] unicodeBytes = unicode.GetBytes(encodedString);
   byte[] asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes);

   // show the MAC address in hex
   Console.WriteLine(BitConverter.ToString(asciiBytes));
   Console.ReadLine();
}
 
Share this answer
 
Comments
Akhand Jyoti 3-Jan-18 4:44am    
Thank u so much for your reply.But this answer do not give what i want.
when i am converting asciiByte to hex i am getting the same result as previous.
after conversion of whole unicode escape sequence result should look like.
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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900