文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>短信猫软件的实现(C#)<九>7bitPDU的编码

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

时间:2010-11-13  来源:给我一杯酒

  1.  
    1. UserData属性:
      UserData仅改动的set访问器以便7bit编码
    2.  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即完成对应部分编码。

    3. PDU7BitEncoder函数:
      新加入7bit编码方法 代码如下:
    4.  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的解码。

    5. 其他改动:
    6. 1:  public string PDUUSC2Encoder(string phone, string Text)

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

  • GSMModem类:
  1.  
    1. 枚举:为了便于区分短信类型(USC2编码还是7bit编码)加入枚举类型
    2. 1:  public enum MsgType { AUSC2, A7Bit };              //枚举 短信类型 AUSC2 A7Bit:7位编码 (中文用AUSC2,英文都可以 但7Bit能发送160字符,USC2仅70)

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

    3. SendMsg重载:
    4.  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类的编码方法的地方做相应改动。

相关阅读 更多 +
排行榜 更多 +
梦幻甜心蛋糕店手游 v1.0 安卓版

梦幻甜心蛋糕店手游 v1.0 安卓版

休闲益智 下载
狙击手血战鬼子 v8081.23.10.7 安卓版

狙击手血战鬼子 v8081.23.10.7 安卓版

休闲益智 下载
狙击手血战鬼子 v8081.23.10.7 安卓版

狙击手血战鬼子 v8081.23.10.7 安卓版

休闲益智 下载