1. 程式人生 > >簡訊貓開發之簡訊編碼格式

簡訊貓開發之簡訊編碼格式

1 ///<summary>2 /// 計算訊息長度
3 ///</summary>4 ///<param name="_phoneNumber">Phone</param>5 ///<param name="_targetText">Context</param>6 ///<returns></returns>7 publicstaticstring CountSMSLength(string _phoneNumber, string _targetText)
8        {
9 if (!(_phoneNumber.Substring(
0, 2) =="86"))
10            {
11                _phoneNumber = String.Format("86{0}", _phoneNumber);     //檢查手機號是否按標準格式書寫,不是,就補上“”12            }
13 return (8+ ((_phoneNumber.Length %2) ==0? _phoneNumber.Length /2 : (_phoneNumber.Length +1) /2) + _targetText.Length *2).ToString();
14        }
15 16 ///<summary>
17 /// 對整個要傳送的簡訊的PDU編碼
18 ///</summary>19 ///<param name="cscaNumber"></param>20 ///<param name="PhoneNumber"></param>21 ///<param name="TargetText"></param>22 ///<returns></returns>23 publicstaticstring PDUEncodingSMS(string _cscaNumber, string _phoneNumber, string _targetText)
24        {
25 if (!(_cscaNumber.Substring(0, 2) =="86"))
26            {
27                _cscaNumber = String.Format("86{0}", _cscaNumber);     //檢查手機號是否按標準格式書寫,不是,就補上“”28            }
29 if (!(_phoneNumber.Substring(0, 2) =="86"))
30            {
31                _phoneNumber = String.Format("86{0}", _phoneNumber);     //檢查手機號是否按標準格式書寫,不是,就補上“”32            }
33 //開始號碼34            StringBuilder _sb =new StringBuilder();
35            _sb.Append("0891");
36 //簡訊中心37            _sb.Append(ReversalNumber(_cscaNumber));
38            _sb.Append("1100"+ String.Format("{0,2:X}", _phoneNumber.Length).Replace('', '0') +"91");
39 //目標號碼40            _sb.Append(ReversalNumber(_phoneNumber));
41 //協議標識,編碼資訊和有效期42            _sb.Append("0008FF");//+ String.Format("{0,2:X}", TargetText.Length * 2).Replace(' ', '0'));
43 //簡訊長度和內容44            _sb.Append(ContentEncoding(_targetText));
45 return _sb.ToString();
46        }
47 48 ///<summary>49 /// 手機號碼的編碼
50 ///</summary>51 ///<param name="Target"></param>52 ///<returns></returns>53 publicstaticstring ReversalNumber(string _phone)
54        {
55 string _strResult =string.Empty;
56 if (!(_phone.Substring(0, 2) =="86"))
57            {
58                _phone = String.Format("86{0}", _phone);     //檢查手機號是否按標準格式書寫,不是,就補上“”59            }
60 61 char[] _eachnumber = (_phone.Length %2==0) ? Convert.ToString(_phone).ToCharArray() : Convert.ToString(_phone +"F").ToCharArray();
62 for (int i =0; i < _eachnumber.Length -1; i +=2)
63            {
64                _strResult += (_eachnumber[i +1].ToString() + _eachnumber[i].ToString());
65            }
66 return _strResult;
67        }
68 69 ///<summary>70 /// 簡訊內容的編碼
71 ///</summary>72 ///<param name="srvContent"></param>73 ///<returns></returns>74 publicstaticstring ContentEncoding(string _srvContent)
75        {
76            Encoding encodingUTF = System.Text.Encoding.BigEndianUnicode;
77 string s =string.Empty;
78 byte[] encodedBytes = encodingUTF.GetBytes(_srvContent);
79 for (int i =0; i < encodedBytes.Length; i++)
80            {
81                s += BitConverter.ToString(encodedBytes, i, 1);
82            }
83            s = String.Format("{0:X2}{1}", s.Length /2, s);
84 85 return s;
86        }
87