PDU解码编码类(转)

  1. public bool ReadSMS(string pduString, out string centerNumber, out string userNumber, out string userMsg)
  2.         {
  3.             centerNumber = userNumber = userMsg = string.Empty;
  4.             try
  5.             {
  6.                 int pos = 0;
  7.                 int cnlength = Convert.ToInt32(pduString.Substring(0, 2), 16); pos += 2;
  8.                 //string cnplus = pduString.Substring(pos, 2);//这个数值肯定是91;表示加号
  9.                 centerNumber = "+" + DecodeNumber(pduString.Substring(pos, 2 * 8)).Remove(0, 2).Replace("F", "");
  10.                 pos += (2 * cnlength);
  11.                 pos += 2;
  12.                 int unlength = Convert.ToInt32(pduString.Substring(pos, 2),16); pos += 2;//用户手机号码长度
  13.                 if (unlength == 13)//正常手机号
  14.                 {
  15.                     userNumber = "+" + DecodeNumber(pduString.Substring(pos, 2 * 8)).Remove(0, 2).Replace("F", "");
  16.                     pos += (2 * 8);
  17.                     pos += 2;//00
  18.                     int codectype = Convert.ToInt32(pduString.Substring(pos, 2), 16); pos += 2;//编码方式 00:7bits 04:8bits 08:USC2
  19.                     pos += 2 * 7;//时间
  20.                     int umlength = Convert.ToInt32(pduString.Substring(pos, 2), 16); pos += 2;
  21.                     switch (codectype)
  22.                     {
  23.                         case 0x00://7bits
  24.                             userMsg = Decode7Bit(pduString.Substring(pos));
  25.                             return true;
  26.                             //break;
  27.                         case 0x08:
  28.                             if (umlength == (pduString.Length - pos) / 2)
  29.                             {
  30.                                 userMsg = DecodeUSC2(pduString.Substring(pos));
  31.                                 return true;
  32.                             }
  33.                             break;
  34.                     }
  35.                 }
  36.             }
  37.             catch { }
  38.             return false;
  39.         }
  40.         public string WriteSMS(string centerNumber, string userNumber, string userMsg, out int length)
  41.         {
  42.             length = 0;
  43.             StringBuilder sb = new StringBuilder();
  44.             if (userNumber.Length != 14||userMsg.Length==0) return null;//+86XXXXXXXXXXX
  45.             string temp = centerNumber.Replace("+", "19");
  46.             if (temp.Length % 2 != 0) temp += "F";//补上F为偶数
  47.             string codecn = EncodeNumber(temp);
  48.             int cnlength=codecn.Length / 2;
  49.             sb.Append(cnlength.ToString("X2"));//长度08
  50.             sb.Append(codecn);//短信中心号码
  51.             sb.Append("1100");
  52.             //用户11位手机号
  53.             sb.Append("0D");//13
  54.             temp = userNumber.Replace("+", "19");
  55.             if (temp.Length % 2 != 0) temp += "F";//补上F为偶数
  56.             string codeun = EncodeNumber(temp);
  57.             sb.Append(codeun);//编码后发送手机号
  58.             sb.Append("0008A7");
  59.             string codeum = EncodeUSC2(userMsg);
  60.             sb.Append((codeum.Length / 2).ToString("X2"));
  61.             sb.Append(codeum);
  62.             string all = sb.ToString();
  63.             length = all.Length / 2 - cnlength - 1;
  64.             return all;
  65.         }
  66.         public string DecodeNumber(string numberString)
  67.         {
  68.             if (numberString.Length % 2 != 0) throw new Exception("传递字符串必须为偶数个");//必须为偶数
  69.             StringBuilder sb=new StringBuilder();
  70.             for (int i = 0; i < numberString.Length; i += 2)//奇偶互换
  71.             {
  72.                 sb.Append(numberString[i+1]);
  73.                 sb.Append(numberString[i]);
  74.             }
  75.             return sb.ToString();
  76.         }
  77.         public string EncodeNumber(string numberString)
  78.         {
  79.             if (numberString.Length % 2 != 0) numberString += "F";//补上F;
  80.             StringBuilder sb = new StringBuilder();
  81.             for (int i = 0; i < numberString.Length; i += 2)//奇偶互换
  82.             {
  83.                 sb.Append(numberString[i + 1]);
  84.                 sb.Append(numberString[i]);
  85.             }
  86.             return sb.ToString();
  87.         }
  88.         public string EncodeUSC2(string contentString)
  89.         {
  90.             string s = null;
  91.             byte[] encodedBytes = Encoding.BigEndianUnicode.GetBytes(contentString);
  92.             for (int i = 0; i < encodedBytes.Length; i++)
  93.             {
  94.                 s += BitConverter.ToString(encodedBytes, i, 1);
  95.             }
  96.             //s = String.Format("{0:X2}{1}", s.Length / 2, s);
  97.             return s;
  98.         }
  99.         public string DecodeUSC2(string contentString)
  100.         {
  101.             string returnStr;
  102.             byte[] strtemp = new byte[contentString.Length / 2];
  103.             int j = contentString.Length;
  104.             for (int i = 0; i < contentString.Length; i += 2)
  105.             {
  106.                 strtemp[i / 2] = (byte)Convert.ToInt32(contentString.Substring(i, 2), 16);
  107.             }
  108.             returnStr = System.Text.Encoding.BigEndianUnicode.GetString(strtemp);
  109.             return returnStr;
  110.         }
  111.         public string Decode7Bit(string contentString)
  112.         {
  113.             int iByte = 0;
  114.             int iLeft = 0;
  115.             //   将源数据每7个字节分为一组,解压缩成8个字节   
  116.             //   循环该处理过程,直至源数据被处理完   
  117.             //   如果分组不到7字节,也能正确处理   
  118.             System.Text.StringBuilder sb = new System.Text.StringBuilder();
  119.             for (int i = 0; i < contentString.Length; i += 2)
  120.             {
  121.                 byte bSrc = byte.Parse(contentString.Substring(i, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
  122.                 //   将源字节右边部分与残余数据相加,去掉最高位,得到一个目标解码字节   
  123.                 sb.Append((((bSrc << iByte) | iLeft) & 0x7f).ToString("X2"));
  124.                 //   将该字节剩下的左边部分,作为残余数据保存起来   
  125.                 iLeft = bSrc >> (7 - iByte);
  126.                 //   修改字节计数值   
  127.                 iByte++;
  128.                 //   到了一组的最后一个字节   
  129.                 if (iByte == 7)
  130.                 {
  131.                     //   额外得到一个目标解码字节   
  132.                     sb.Append(iLeft.ToString("X2"));
  133.                     //   组内字节序号和残余数据初始化   
  134.                     iByte = 0;
  135.                     iLeft = 0;
  136.                 }
  137.             }
  138.             string sReturn = sb.ToString();
  139.             byte[] buf = new byte[sReturn.Length / 2];
  140.             for (int i = 0; i < sReturn.Length; i += 2)
  141.             {
  142.                 buf[i / 2] = byte.Parse(sReturn.Substring(i, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
  143.             }
  144.             return System.Text.Encoding.ASCII.GetString(buf);
  145.         }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值