- public bool ReadSMS(string pduString, out string centerNumber, out string userNumber, out string userMsg)
- {
- centerNumber = userNumber = userMsg = string.Empty;
- try
- {
- int pos = 0;
- int cnlength = Convert.ToInt32(pduString.Substring(0, 2), 16); pos += 2;
- //string cnplus = pduString.Substring(pos, 2);//这个数值肯定是91;表示加号
- centerNumber = "+" + DecodeNumber(pduString.Substring(pos, 2 * 8)).Remove(0, 2).Replace("F", "");
- pos += (2 * cnlength);
- pos += 2;
- int unlength = Convert.ToInt32(pduString.Substring(pos, 2),16); pos += 2;//用户手机号码长度
- if (unlength == 13)//正常手机号
- {
- userNumber = "+" + DecodeNumber(pduString.Substring(pos, 2 * 8)).Remove(0, 2).Replace("F", "");
- pos += (2 * 8);
- pos += 2;//00
- int codectype = Convert.ToInt32(pduString.Substring(pos, 2), 16); pos += 2;//编码方式 00:7bits 04:8bits 08:USC2
- pos += 2 * 7;//时间
- int umlength = Convert.ToInt32(pduString.Substring(pos, 2), 16); pos += 2;
- switch (codectype)
- {
- case 0x00://7bits
- userMsg = Decode7Bit(pduString.Substring(pos));
- return true;
- //break;
- case 0x08:
- if (umlength == (pduString.Length - pos) / 2)
- {
- userMsg = DecodeUSC2(pduString.Substring(pos));
- return true;
- }
- break;
- }
- }
- }
- catch { }
- return false;
- }
- public string WriteSMS(string centerNumber, string userNumber, string userMsg, out int length)
- {
- length = 0;
- StringBuilder sb = new StringBuilder();
- if (userNumber.Length != 14||userMsg.Length==0) return null;//+86XXXXXXXXXXX
- string temp = centerNumber.Replace("+", "19");
- if (temp.Length % 2 != 0) temp += "F";//补上F为偶数
- string codecn = EncodeNumber(temp);
- int cnlength=codecn.Length / 2;
- sb.Append(cnlength.ToString("X2"));//长度08
- sb.Append(codecn);//短信中心号码
- sb.Append("1100");
- //用户11位手机号
- sb.Append("0D");//13
- temp = userNumber.Replace("+", "19");
- if (temp.Length % 2 != 0) temp += "F";//补上F为偶数
- string codeun = EncodeNumber(temp);
- sb.Append(codeun);//编码后发送手机号
- sb.Append("0008A7");
- string codeum = EncodeUSC2(userMsg);
- sb.Append((codeum.Length / 2).ToString("X2"));
- sb.Append(codeum);
- string all = sb.ToString();
- length = all.Length / 2 - cnlength - 1;
- return all;
- }
- public string DecodeNumber(string numberString)
- {
- if (numberString.Length % 2 != 0) throw new Exception("传递字符串必须为偶数个");//必须为偶数
- StringBuilder sb=new StringBuilder();
- for (int i = 0; i < numberString.Length; i += 2)//奇偶互换
- {
- sb.Append(numberString[i+1]);
- sb.Append(numberString[i]);
- }
- return sb.ToString();
- }
- public string EncodeNumber(string numberString)
- {
- if (numberString.Length % 2 != 0) numberString += "F";//补上F;
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < numberString.Length; i += 2)//奇偶互换
- {
- sb.Append(numberString[i + 1]);
- sb.Append(numberString[i]);
- }
- return sb.ToString();
- }
- public string EncodeUSC2(string contentString)
- {
- string s = null;
- byte[] encodedBytes = Encoding.BigEndianUnicode.GetBytes(contentString);
- for (int i = 0; i < encodedBytes.Length; i++)
- {
- s += BitConverter.ToString(encodedBytes, i, 1);
- }
- //s = String.Format("{0:X2}{1}", s.Length / 2, s);
- return s;
- }
- public string DecodeUSC2(string contentString)
- {
- string returnStr;
- byte[] strtemp = new byte[contentString.Length / 2];
- int j = contentString.Length;
- for (int i = 0; i < contentString.Length; i += 2)
- {
- strtemp[i / 2] = (byte)Convert.ToInt32(contentString.Substring(i, 2), 16);
- }
- returnStr = System.Text.Encoding.BigEndianUnicode.GetString(strtemp);
- return returnStr;
- }
- public string Decode7Bit(string contentString)
- {
- int iByte = 0;
- int iLeft = 0;
- // 将源数据每7个字节分为一组,解压缩成8个字节
- // 循环该处理过程,直至源数据被处理完
- // 如果分组不到7字节,也能正确处理
- System.Text.StringBuilder sb = new System.Text.StringBuilder();
- for (int i = 0; i < contentString.Length; i += 2)
- {
- byte bSrc = byte.Parse(contentString.Substring(i, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
- // 将源字节右边部分与残余数据相加,去掉最高位,得到一个目标解码字节
- sb.Append((((bSrc << iByte) | iLeft) & 0x7f).ToString("X2"));
- // 将该字节剩下的左边部分,作为残余数据保存起来
- iLeft = bSrc >> (7 - iByte);
- // 修改字节计数值
- iByte++;
- // 到了一组的最后一个字节
- if (iByte == 7)
- {
- // 额外得到一个目标解码字节
- sb.Append(iLeft.ToString("X2"));
- // 组内字节序号和残余数据初始化
- iByte = 0;
- iLeft = 0;
- }
- }
- string sReturn = sb.ToString();
- byte[] buf = new byte[sReturn.Length / 2];
- for (int i = 0; i < sReturn.Length; i += 2)
- {
- buf[i / 2] = byte.Parse(sReturn.Substring(i, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
- }
- return System.Text.Encoding.ASCII.GetString(buf);
- }
PDU解码编码类(转)
最新推荐文章于 2024-04-01 09:23:30 发布