将7位编码字符串隐藏为纯文本

时间:2019-01-18 12:50:28

标签: c# encoding decode gsm at-command

我正在尝试从D-link DWM-157 HSPA + USB适配器读取SMS。我可以通过AT命令阅读SMS。当SMS包含的字符少于160个时,该代码可以正常工作。如果SMS包含多部分字符,它将转换为7位格式,我现在可以使用this库对其进行解码。 Decode7bit()方法可以正常工作,但是当字符串(此方法的参数)长度为268时,但是当长度小于268时,它会给出垃圾值。代码是

  public static byte[] GetInvertBytes(string source)
    {
        byte[] bytes = GetBytes(source);

        Array.Reverse(bytes);

        return bytes;
    }
    public static byte[] GetBytes(string source, int fromBase)
    {
        List<byte> bytes = new List<byte>();

        for (int i = 0; i < source.Length / 2; i++)
            bytes.Add(Convert.ToByte(source.Substring(i * 2, 2), fromBase));

        return bytes.ToArray();
    }

    public static byte[] GetBytes(string source)
    {
        return GetBytes(source, 16);
    }

    public static string Decode7bit(string source, int length)
    {
        byte[] bytes = GetInvertBytes(source);

        string binary = string.Empty;

        foreach (byte b in bytes)
            binary += Convert.ToString(b, 2).PadLeft(8, '0');

        binary = binary.PadRight(length * 7, '0');

        string result = string.Empty;

        for (int i = 1; i <= length; i++)
            result += (char)Convert.ToByte(binary.Substring(binary.Length - i * 7, 7), 2);

        //return result.Replace('\x0', '\x40');
        return result.Replace("\0", "");
    }

我从GSM模块获取短信的代码是

 public string Read()
    {
        //Console.WriteLine("Reading..");

        gsmPort.WriteLine("AT+CMGF=1"); // Set mode to Text(1) or PDU(0)
        Thread.Sleep(1000); // Give a second to write
        //gsmPort.WriteLine("AT+CPMS=\"SM\""); // Set storage to SIM(SM)
        gsmPort.WriteLine("AT+CPMS=\"ME\",\"SM\",\"MT\""); // Set storage to SIM(SM)

        Thread.Sleep(1000);
       // gsmPort.WriteLine("AT+CMGL=\"REC UNREAD\""); // What category to read ALL, REC READ, or REC UNREAD
        gsmPort.WriteLine("AT+CMGL=\"ALL\""); // What category to read ALL, REC READ, or REC UNREAD
        Thread.Sleep(1000);
        gsmPort.Write("\r");
        Thread.Sleep(1000);


        string response = gsmPort.ReadExisting();




        if (response.EndsWith("\r\nOK\r\n\r") || response.EndsWith("\r\nOK\r\n"))
        { //there is no error
            //Console.WriteLine(response);
            return response;
            // add more code here to manipulate reponse string.
        }
        else
        {
            return "ERROR";
            // add more code here to handle error.
            //Console.WriteLine(response);
        }

    }

我搜索了7位解码器,但无法获得任何合适的结果。谁能告诉我如何将7位字符串转换为纯文本,或任何其他方法来读取没有7位编码或执行上述操作的任何AT命令的多部分SMS。

0 个答案:

没有答案