PDU串发送长短信

今天先把代码发上来,以后再解说:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
  1ExpandedBlockStart.gifContractedBlock.gif /**//// <summary>
  2    /// 针对国内短信编码(USC2)
  3    /// </summary>

  4    public class PDUdecoding
  5ExpandedBlockStart.gifContractedBlock.gif    {
  6        public readonly static int MAX_CHAR_COUNT = 70;//最长可发送汉字个数
  7
  8ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
  9        /// 奇偶互换并补F
 10        /// </summary>
 11        /// <param name="value"></param>
 12        /// <returns></returns>

 13        private static string ParityChange(string value)
 14ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 15            string result = string.Empty;
 16            int length = value.Length;
 17            for (int i = 1; i < length; i += 2)//奇偶互换
 18ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 19                result += value[i];
 20                result += value[i - 1];
 21            }

 22
 23            if (!(length % 2 == 0))//不是偶数则加上F,与最后一位互换
 24ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 25                result += 'F';
 26                result += value[length - 1];
 27            }

 28            return result;
 29        }

 30
 31ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 32        /// 短信内容编码
 33        /// </summary>
 34        /// <param name="value"></param>
 35        /// <returns></returns>
 36        /// <remarks>
 37        /// 采用Big-Endian 字节顺序的 Unicode 格式编码,将高低位互换
 38        /// 将转换后的短信内容存进字节数组
 39        /// 去掉在进行Unicode格式编码中,两个字节中的"-",例如:00-21,变成0021
 40        /// 将整条短信内容的长度除2,保留两位16进制数
 41        /// </remarks>

 42        public static string Encoding(string value)
 43ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 44            Encoding encoding = System.Text.Encoding.BigEndianUnicode;
 45            string result = string.Empty;
 46            byte[] bytes = encoding.GetBytes(value);
 47
 48            for (int i = 0; i < bytes.Length; i++)
 49ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 50                result += BitConverter.ToString(bytes, i, 1);
 51            }

 52
 53            return result;
 54        }

 55
 56        public static string EncodeingAddLength(string value)
 57ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 58            string result = Encoding(value);
 59            return  String.Format("{0:X2}{1}", result.Length / 2, result);
 60        }

 61
 62ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 63        /// 短信中心号码编码
 64        /// </summary>
 65        /// <param name="value"></param>
 66        /// <returns></returns>

 67        public static string DecodingCenter(string phone)
 68ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 69            string result = string.Empty;
 70            result = ParityChange(phone);
 71
 72            result = String.Format("91{0}", result);//加上91
 73            result = String.Format("{0:X2}{1}", result.Length / 2, result);
 74            return result;
 75        }

 76
 77ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 78        /// 接收短信手机号码编码
 79        /// </summary>
 80        /// <param name="phone"></param>
 81        /// <returns></returns>

 82        public static string DecodingPhone(string phone)
 83ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 84            string result = string.Empty;
 85
 86            if (null == phone || 0 == phone.Length)
 87ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 88                return result;
 89            }

 90
 91            if ('+' == phone[0])
 92ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 93                phone = phone.TrimStart('+');
 94            }

 95
 96            if (!(phone.Substring(02== "86"))//补86
 97ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 98                phone = String.Format("86{0}", phone);
 99            }

100
101            return ParityChange(phone);
102        }

103
104ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
105        /// 整个短信的编码
106        /// </summary>
107        /// <param name="center"></param>
108        /// <param name="phone"></param>
109        /// <param name="content"></param>
110        /// <param name="length">要发送内容的长度,由两部分组成,接收手机号加上要发送的内容</param>
111        /// <returns></returns>

112        public static string EncodingSMS(string center, string phone, string content, out string length)
113ExpandedSubBlockStart.gifContractedSubBlock.gif        {
114            center = DecodingCenter(center);
115            string result = String.Format("{0}11000D91{1}000800{2}", center, DecodingPhone(phone), EncodeingAddLength(content));
116            length = String.Format("{0:D2}", result.Length / 2 - center.Length / 2);//获取短信内容加上手机号码长度
117            return result;
118        }

119
120ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
121        /// 超长短信
122        /// </summary>
123        /// <param name="center"></param>
124        /// <param name="phone"></param>
125        /// <param name="content"></param>
126        /// <param name="count"></param>
127        /// <param name="i"></param>
128        /// <param name="length"></param>
129        /// <returns></returns>

130        public static string EncodingSMS(string center, string phone, string content,int count,int i, out string length)
131ExpandedSubBlockStart.gifContractedSubBlock.gif        {
132            if (content.Length <= PDUdecoding.MAX_CHAR_COUNT)
133ExpandedSubBlockStart.gifContractedSubBlock.gif            {
134                return PDUdecoding.EncodingSMS(center, phone, content, out length);
135            }

136            else
137ExpandedSubBlockStart.gifContractedSubBlock.gif            {
138                if (count - 1 == i)
139ExpandedSubBlockStart.gifContractedSubBlock.gif                {
140                    content = content.Substring(i * (PDUdecoding.MAX_CHAR_COUNT-6));
141                }

142                else
143ExpandedSubBlockStart.gifContractedSubBlock.gif                {
144                    content = content.Substring(i * (PDUdecoding.MAX_CHAR_COUNT-6), PDUdecoding.MAX_CHAR_COUNT-6);
145                }

146
147                center = DecodingCenter(center);
148                content = Encoding(content);
149
150                string result = "";
151
152                DateTime tm = DateTime.Now;
153
154                //result = string.Format("{0}44000D91{1}0008{2}{3:X2}05000304{4:D2}{5:D2}{6}",
155                //                        center,
156                //                        DecodingPhone(phone),
157                //                        ParityChange(string.Format("{0:X2}{1:X2}{2:X2}{3:X2}{4:X2}{5:X2}08", tm.Year - 2000, tm.Month, tm.Day, tm.Hour, tm.Minute, tm.Second)),
158                //                        (content.Length + 12) / 2,
159                //                        count,
160                //                        i + 1,
161                //                        content
162                //                        );
163
164                result = string.Format("005100{0}{1}0008A7{2:X2}05000304{3:D2}{4:D2}{5}",
165                                        center,
166                                        DecodingPhone(phone),
167                                        (content.Length + 12/ 2,
168                                        count,
169                                        i + 1,
170                                        content);
171
172                length = String.Format("{0:D2}", result.Length / 2 - center.Length / 2);//获取短信内容加上手机号码长度
173                return result;
174            }

175        }

176
177        public static int GetMaxEncodeCharCount()
178ExpandedSubBlockStart.gifContractedSubBlock.gif        {
179            return PDUdecoding.MAX_CHAR_COUNT * 2 - 6;
180        }

181ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
182        /// 
183        /// </summary>
184        /// <param name="center"></param>
185        /// <param name="phone"></param>
186        /// <param name="count"></param>
187        /// <param name="i"></param>
188        /// <param name="content"></param>
189        /// <param name="length"></param>
190        /// <returns></returns>

191        public static string EncodingSMS(string center, string phone, int count, int i, string content, out string length)
192ExpandedSubBlockStart.gifContractedSubBlock.gif        {
193            string message = string.Empty;
194            int msgcount = PDUdecoding.GetMaxEncodeCharCount() * 2;//固定
195
196            if (i == count)
197ExpandedSubBlockStart.gifContractedSubBlock.gif            {
198                message = content.Substring((i - 1* msgcount); //共可发送134个编码
199            }

200            else
201ExpandedSubBlockStart.gifContractedSubBlock.gif            {
202                message = content.Substring((i - 1* msgcount, msgcount);
203            }

204
205            center = DecodingCenter(center);
206
207            //string result = string.Format("005100{0}{1}0008A7{2:X2}05000304{3:D2}{4:D2}{5}",
208            //                            center,
209            //                            DecodingPhone(phone),
210            //                            (message.Length + 12) / 2,
211            //                            count,
212            //                            i,
213            //                            message);
214
215            //length = String.Format("{0:D2}", result.Length / 2 - center.Length / 2);//获取短信内容加上手机号码长度
216
217ExpandedSubBlockStart.gifContractedSubBlock.gif            /**/////string ph = phone;
218            ////phone = DecodingPhone(phone).Substring(2);
219
220            ////string result = string.Format("005100{0}{1}0008A7{2:X2}05000304{3:D2}{4:D2}{5}",
221            ////                              string.Format("{0:X2}A1", ph.Length),
222            ////                            phone,
223            ////                            (message.Length + 12) / 2,
224            ////                            count,
225            ////                            i,
226            ////                            message);

227
228            string result = String.Format("{0}51000D91{1}000800{2:X2}05000304{4:D2}{5:D2}{3}",
229                                            center, 
230                                            DecodingPhone(phone),
231                                            message.Length/2 + 6,
232                                            message,
233                                            count,
234                                            i
235                                            );
236
237            length = String.Format("{0:D2}", result.Length / 2 - center.Length / 2);//获取短信内容加上手机号码长度
238
239            return result;
240        }

241
242        public static string EncodingOther(string content)
243ExpandedSubBlockStart.gifContractedSubBlock.gif        {
244            string result = string.Empty;
245            byte[] bytes = System.Text.Encoding.BigEndianUnicode.GetBytes(content);
246
247            //for (int i = 0; i < bytes.Length; i++)
248            //{
249            //    if (0x80 == (bytes[i] & 0x80))//汉字 6c49  108 73
250            //    {
251            //        result = string.Format("{0}{1:X2}", result, bytes[i]);
252            //        result = string.Format("{0}{1:X2}", result, bytes[++i]);
253            //    }
254            //    else
255            //    {
256            //        result = string.Format("{0}00{1:X2}", result, bytes[i]);
257            //    }
258            //}
259            foreach (char item in content)
260ExpandedSubBlockStart.gifContractedSubBlock.gif            {
261ExpandedSubBlockStart.gifContractedSubBlock.gif                bytes = System.Text.Encoding.BigEndianUnicode.GetBytes(new char[1]{item});
262                if (bytes.Length < 2)
263                    continue;
264
265                //
266                if (0x80 ==( Asc(item) & 0x80))//汉字
267ExpandedSubBlockStart.gifContractedSubBlock.gif                {
268                    result = string.Format("{0}{1:X2}", result, bytes[0]);
269                    result = string.Format("{0}{1:X2}", result, bytes[1]);
270                }

271                else
272ExpandedSubBlockStart.gifContractedSubBlock.gif                {
273                    result = string.Format("{0}00{1:X2}", result, bytes[1]);
274                }

275            }

276            return result;
277        }

278
279        private static int Asc(char item)
280ExpandedSubBlockStart.gifContractedSubBlock.gif        {
281ExpandedSubBlockStart.gifContractedSubBlock.gif            byte[] bytes = System.Text.Encoding.Default.GetBytes(new char[1{ item });
282
283            if (bytes.Length  < 2)
284                return bytes[0];
285
286            return bytes[0* 256 + bytes[1- 65535;
287        }

288    }

 

发送长短信时需要先调用EncodingOther对字符进行编码,然后调用EncodingSMS第二个重载方法发送。

 

发送长短信时需要调用

转载于:https://www.cnblogs.com/skynice/archive/2008/11/26/LongSMSPDU.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值