短信猫软件的实现(C#)<九>7bitPDU的编码

前一段时间完成了一个简单的类库,虽然可以完成可以完成短信猫短信的发送与接收,但是类库还不是很完善。
如:英文短信的发送用的是USC2编码,每条短信最多可发送70字符。而如果用7bit编码则每条能发送160字符 ,本文即在之前类的基础上加入了PDU的编码。

UserData属性:
UserData仅改动的set访问器以便7bit编码
编码算法:
高低交换,从高每8位取为一个字符即可。例:
Test:
        T:01010100  e:01100101  s:01110011  t:01110100
    去最高位0,变为7位
    T:1010100  e:1100101  s:1110011  t:1110100
    高低位交换:tseT
    1110100 1110011 1100101 1010100
    从高位每8位取为一个位组:1 1010100  11 110010  100 11100  0000 1110
    D4F29C0E  完成编码!
源代码如下:

 1:  set
 2:   {
 3:      if (DataCodingScheme == "08" || DataCodingScheme == "18")           //USC2编码使用 原来部分:完成USC2
  4:      {
 5:          userData = string.Empty;
 6:          Encoding encodingUTF = Encoding.BigEndianUnicode;
 7:   
 8:          byte[] Bytes = encodingUTF.GetBytes(value);
 9:   
10:          for (int i = 0; i < Bytes.Length; i++)
11:          {
12:              userData += BitConverter.ToString(Bytes, i, 1);
13:          }
14:          userDataLenghth = (userData.Length / 2).ToString("X2");
15:      }
16:      else                                                                //7bit编码使用  添加内容 利于7bit编码
17:      {
18:          userData = string.Empty;
19:          userDataLenghth = value.Length.ToString("X2");                  //7bit编码 用户数据长度:源字符串长度
20:   
21:          Encoding encodingAsscii = Encoding.ASCII;
22:          byte[] bytes = encodingAsscii.GetBytes(value);
23:   
24:          string temp = string.Empty;                                     //存储中间字符串 二进制串
25:          string tmp;
26:          for (int i = value.Length; i > 0; i--)                          //高低交换 二进制串
27:          {
28:              tmp = Convert.ToString(bytes[i - 1], 2);
29:              while (tmp.Length < 7)                                      //不够7位,补齐
30:              {
31:                  tmp = "0" + tmp;
32:              }
33:              temp += tmp;
34:          }
35:   
36:          for (int i = temp.Length ; i > 0; i -= 8)                    //每8位取位一个字符 即完成编码 同时高位取为低位
37:          {
38:              if (i > 8)
39:              {
40:                  userData += Convert.ToInt32(temp.Substring(i-8, 8), 2).ToString("X2");
41:              }
42:              else
43:              {
44:                  userData += Convert.ToInt32(temp.Substring(0, i), 2).ToString("X2");
45:              }
46:          }
47:   
48:      }
49:  }

加入后只需读取字段userData即完成对应部分编码。

PDU7BitEncoder函数:
新加入7bit编码方法 代码如下:

 1:  public string PDU7BitEncoder(string phone, string Text)
 2:  {
 3:      if (Text.Length > 160)
 4:      {
 5:          throw new Exception("短信字数大于160");
 6:      }
 7:      dataCodingScheme = "00";
 8:      DestinationAddress = phone;
 9:      UserData = Text;
10:   
11:      return serviceCenterAddress + protocolDataUnitType
12:          + messageReference + destinationAddress + protocolIdentifer
13:          + dataCodingScheme + validityPeriod + userDataLenghth + userData;
14:  }

方法只完成对应属性的赋值,直接返回字段字符串即完成7bit的解码。

其他改动:

1:  public string PDUUSC2Encoder(string phone, string Text)

原来的方法名由PDUEncoder改为PDUUSC2Encoder,以是程序拥有更好的可读性。

  • GSMModem类:

枚举:为了便于区分短信类型(USC2编码还是7bit编码)加入枚举类型

1:  public enum MsgType { AUSC2, A7Bit };              //枚举 短信类型 AUSC2 A7Bit:7位编码 (中文用AUSC2,英文都可以 但7Bit能发送160字符,USC2仅70)

在命名空间GSMMODEM中加入枚举类型MsgType

SendMsg重载:

 1:  public void SendMsg(string phone, string msg, MsgType msgType)
 2:  {
 3:      if (msgType == MsgType.AUSC2)
 4:      {
 5:          SendMsg(phone, msg);
 6:      }
 7:      else
 8:      {
 9:   
10:      }
11:  }

如果类型为AUSC2,调用原来的发送方法 用USC2编码发送短信,如果是A7Bit类型用PDUEncoding类的7bit编码函数,程序尚未实现,将在下篇博客中给出详细源代码
当然,原来的调用PDU类的编码方法的地方做相应改动。

附件:工程项目文件

posted @ 2010-11-13 22:37  给我一杯酒  阅读(4679)  评论(0编辑  收藏  举报