Search code examples
c#decodeat-commandussducs2

USSD received message decoding


I'm sending AT+CUSD command to a modem and when i receive this message i can't understand it nor decoding it in any way please i need help. this is a sample

AT+CUSD=1,"*100#",15
+CUSD: 0,"Service not allowed.",15
OK
AT+CUSD=1,"*155#1#",15
+CUSD: 0,"0645063306280642002006270644062F06410639003A00200037002C003600320035002E0030003000200020000A06270644063506440627062D064A0629003A0030002E0030003000200020000A00200627064406440627062D0642002006270644062F06410639003A0030002E003000300020",72
OK

you see first is readible and dcs is 15 but the other one isn't and tha dcs is 72 help !!


Solution

  • What do you get when you decode it using Unicode? I can see a lot of 06's, and according to Wikipedia, standard Arabic is encoded from 0600 - 06FF. S List

    The answer thanks to S List

    is that I need to decode every four letters like "0645" using "UCS2" encoding.

    and my code is like this :

    String origin = "0645063306280642002006270644062F06410639003A00200037002C003600320035002E0030003000200020000A06270644063506440627062D064A0629003A0030002E0030003000200020000A00200627064406440627062D0642002006270644062F06410639003A0030002E003000300020";
    if (origin.Count() % 2 == 0)
                {
                    List<short> list = new List<short>();
                    List<byte> bytes = new List<byte>();
                    var encode = Encoding.GetEncoding("UCS-2");
                    for (int i = 0; i < origin.Count(); i += 4)
                    {
                        list.Add(Convert.ToInt16(origin.Substring(i, 4), 16));
                    }
                    foreach (var item in list)
                    {
                        bytes.Add((byte)(item & 255));
                        bytes.Add((byte)(item >> 8));
                    }
                    return encode.GetString(bytes.ToArray());
                }