C#:PDU格式短信编解码器

版本历史:

  • 2016年12月07日 V1.1发布
  • 2011年08月19日 V1.0发布

功能描述:

  • 短信编码方面,支持BIT7、BIT8、UCS2、AUTO数据编码方案,支持长短信自动拆分,提供短信有效期、拒绝复本、请求状态报告等多项参数设置
  • 短信解码方面,完整呈现所有细节信息

源代码出售:

sell
价格:壹佰元人民币
微信:splashcn

演示程序下载:

SMSPDU.zip

演示程序界面:
短信内容:hellohello
编码结果:0891683108100005F011000B818116043482F10000A70AE8329BFD4697D9EC37

解码数据:07917283010010F5040BC87238880900F10000993092516195800AE8329BFD4697D9EC37
短信内容:hellohello

演示程序源代码:
Form1.cs

using Com.FirstSolver.Splash;
using System;
using System.Text;
using System.Windows.Forms;

namespace Splash
{
    public partial class Form1 : Form
    {
        private SMS Codec = new SMS();
        private SMS.EnumDCS DCS = SMS.EnumDCS.AUTO;

        public Form1()
        {
            InitializeComponent();
        }

        // 编码字符集
        private void rbCharset_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton rb = sender as RadioButton;
            if (rb.Checked)
            {
                if (rb.Equals(rbGSM))
                    DCS = SMS.EnumDCS.BIT7;
                else if (rb.Equals(rbASCII))
                    DCS = SMS.EnumDCS.BIT8;
                else if (rb.Equals(rbUCS2))
                    DCS = SMS.EnumDCS.UCS2;
                else
                    DCS = SMS.EnumDCS.AUTO;
            }
        }

        // 短信中心
        private void rbSCA_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton rb = sender as RadioButton;
            if (rb.Checked)
            {
                if (rb.Equals(rbCM))
                {   // 中国移动(北京)
                    textBoxSCA.Text = "+8613800100500";
                }
                else
                {   // 中国联通(北京)
                    textBoxSCA.Text = "+8613010112500";
                }
            }
        }

        // 清空
        private void buttonClear_Click(object sender, EventArgs e)
        {
            textBoxMessage.Clear();
            textBoxPDU.Clear();
        }

        // 编码
        private void buttonEncode_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBoxMessage.Text) || string.IsNullOrEmpty(textBoxPhone.Text)) return;
            try
            {
                string[] Series;
                if (DCS.Equals(SMS.EnumDCS.BIT8))
                {   // 二进制数据,先进行BASE64解码
                    Series = Codec.PDUEncoding(textBoxSCA.Text, textBoxPhone.Text, Convert.FromBase64String(textBoxMessage.Text.Trim()), null, DCS);
                }
                else
                {   // 文本串
                    Series = Codec.PDUEncoding(textBoxSCA.Text, textBoxPhone.Text, textBoxMessage.Text.Trim(), null, DCS);
                }

                StringBuilder sb = new StringBuilder();
                foreach (string item in Series)
                {
                    sb.Append(item);
                    sb.Append("\r\n\r\n");
                }

                if (sb.Length > 4) sb.Remove(sb.Length - 4, 4);
                textBoxPDU.Text = sb.ToString();
            }
            catch
            {
                MessageBoxPlus.Show(this, "生成数据包失败!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // 解码
        private void buttonDecode_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBoxPDU.Text)) return;

            try
            {
                SMS.SMSPARTS Parts = SMS.PDUDecoding(textBoxPDU.Text.Trim());

                string Message;
                if (Parts.UD is string)
                {   // 文本串
                    Message = Parts.UD as string;
                }
                else
                {   // 二进制数据转换为BASE64编码字符串输出
                    Message = Convert.ToBase64String(Parts.UD as byte[]);
                }

                StringBuilder sb = new StringBuilder();
                sb.Append("解码内容:");
                sb.Append("\r\n");

                sb.Append("服务中心地址:" + Parts.SCA);
                sb.Append("\r\n");

                sb.Append("发送方地址:" + Parts.OA);
                sb.Append("\r\n");

                sb.Append("服务中心时间戳:" + Parts.SCTS.ToString());
                sb.Append("\r\n");

                sb.Append("用户数据:" + Message);
                sb.Append("\r\n");

                sb.Append("应答路径:" + Parts.RP.ToString());
                sb.Append("\r\n");

                sb.Append("用户数据头标识:" + Parts.UDHI.ToString());
                sb.Append("\r\n");

                sb.Append("状态报告指示:" + Parts.SRI.ToString());
                sb.Append("\r\n");

                sb.Append("更多信息发送:" + Parts.MMS.ToString());
                sb.Append("\r\n");

                sb.Append("信息类型指示:" + Parts.MTI.ToString());
                sb.Append("\r\n");

                sb.Append("PID协议标识:" + Parts.PID.ToString("X2"));
                sb.Append("\r\n");

                sb.Append("数据编码方案:" + Parts.DCS.ToString());
                sb.Append("\r\n");

                sb.Append("文本压缩指示:" + Parts.TC.ToString());
                sb.Append("\r\n");

                sb.Append("消息类型:" + Parts.MC.ToString());
                sb.Append("\r\n");

                if (Parts.UDH != null)
                {
                    sb.Append("用户数据头信息:");
                    sb.Append("\r\n");

                    foreach (SMS.PDUUDH Element in Parts.UDH)
                    {
                        sb.Append("信息元素标识:" + Element.IEI.ToString("X2"));
                        sb.Append("\r\n");

                        sb.Append("信息元素数据:");
                        foreach (byte b in Element.IED)
                        {
                            sb.Append(b.ToString("X2"));
                        }
                        sb.Append("\r\n");
                    }
                }

                textBoxMessage.Text = sb.ToString();
            }
            catch
            {
                MessageBoxPlus.Show(this, "还原数据包失败!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}

Form1.Designer.cs

namespace Splash
{
    partial class Form1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要修改
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.rbAuto = new System.Windows.Forms.RadioButton();
            this.rbUCS2 = new System.Windows.Forms.RadioButton();
            this.rbASCII = new System.Windows.Forms.RadioButton();
            this.rbGSM = new System.Windows.Forms.RadioButton();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.rbCU = new System.Windows.Forms.RadioButton();
            this.textBoxSCA = new System.Windows.Forms.TextBox();
            this.rbCM = new System.Windows.Forms.RadioButton();
            this.groupBox3 = new System.Windows.Forms.GroupBox();
            this.textBoxPhone = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.groupBox4 = new System.Windows.Forms.GroupBox();
            this.textBoxMessage = new System.Windows.Forms.TextBox();
            this.groupBox5 = new System.Windows.Forms.GroupBox();
            this.textBoxPDU = new System.Windows.Forms.TextBox();
            this.buttonClear = new System.Windows.Forms.Button();
            this.buttonEncode = new System.Windows.Forms.Button();
            this.buttonDecode = new System.Windows.Forms.Button();
            this.groupBox1.SuspendLayout();
            this.groupBox2.SuspendLayout();
            this.groupBox3.SuspendLayout();
            this.groupBox4.SuspendLayout();
            this.groupBox5.SuspendLayout();
            this.SuspendLayout();
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.rbAuto);
            this.groupBox1.Controls.Add(this.rbUCS2);
            this.groupBox1.Controls.Add(this.rbASCII);
            this.groupBox1.Controls.Add(this.rbGSM);
            this.groupBox1.Location = new System.Drawing.Point(12, 12);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(420, 78);
            this.groupBox1.TabIndex = 0;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "编码字符集";
            // 
            // rbAuto
            // 
            this.rbAuto.AutoSize = true;
            this.rbAuto.Checked = true;
            this.rbAuto.Location = new System.Drawing.Point(270, 22);
            this.rbAuto.Name = "rbAuto";
            this.rbAuto.Size = new System.Drawing.Size(71, 16);
            this.rbAuto.TabIndex = 3;
            this.rbAuto.TabStop = true;
            this.rbAuto.Text = "最佳编码";
            this.rbAuto.UseVisualStyleBackColor = true;
            this.rbAuto.CheckedChanged += new System.EventHandler(this.rbCharset_CheckedChanged);
            // 
            // rbUCS2
            // 
            this.rbUCS2.AutoSize = true;
            this.rbUCS2.Location = new System.Drawing.Point(156, 22);
            this.rbUCS2.Name = "rbUCS2";
            this.rbUCS2.Size = new System.Drawing.Size(47, 16);
            this.rbUCS2.TabIndex = 2;
            this.rbUCS2.Text = "UCS2";
            this.rbUCS2.UseVisualStyleBackColor = true;
            this.rbUCS2.CheckedChanged += new System.EventHandler(this.rbCharset_CheckedChanged);
            // 
            // rbASCII
            // 
            this.rbASCII.AutoSize = true;
            this.rbASCII.Location = new System.Drawing.Point(18, 50);
            this.rbASCII.Name = "rbASCII";
            this.rbASCII.Size = new System.Drawing.Size(395, 16);
            this.rbASCII.TabIndex = 1;
            this.rbASCII.Text = "ASCII/BIT8(只适合二进制数据,短信内容必须为BASE64编码字符串)";
            this.rbASCII.UseVisualStyleBackColor = true;
            this.rbASCII.CheckedChanged += new System.EventHandler(this.rbCharset_CheckedChanged);
            // 
            // rbGSM
            // 
            this.rbGSM.AutoSize = true;
            this.rbGSM.Location = new System.Drawing.Point(18, 22);
            this.rbGSM.Name = "rbGSM";
            this.rbGSM.Size = new System.Drawing.Size(71, 16);
            this.rbGSM.TabIndex = 0;
            this.rbGSM.Text = "GSM/BIT7";
            this.rbGSM.UseVisualStyleBackColor = true;
            this.rbGSM.CheckedChanged += new System.EventHandler(this.rbCharset_CheckedChanged);
            // 
            // groupBox2
            // 
            this.groupBox2.Controls.Add(this.rbCU);
            this.groupBox2.Controls.Add(this.textBoxSCA);
            this.groupBox2.Controls.Add(this.rbCM);
            this.groupBox2.Location = new System.Drawing.Point(12, 99);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(419, 54);
            this.groupBox2.TabIndex = 1;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "短消息中心";
            // 
            // rbCU
            // 
            this.rbCU.AutoSize = true;
            this.rbCU.Location = new System.Drawing.Point(126, 23);
            this.rbCU.Name = "rbCU";
            this.rbCU.Size = new System.Drawing.Size(71, 16);
            this.rbCU.TabIndex = 3;
            this.rbCU.Text = "中国联通";
            this.rbCU.UseVisualStyleBackColor = true;
            this.rbCU.CheckedChanged += new System.EventHandler(this.rbSCA_CheckedChanged);
            // 
            // textBoxSCA
            // 
            this.textBoxSCA.Location = new System.Drawing.Point(246, 21);
            this.textBoxSCA.Name = "textBoxSCA";
            this.textBoxSCA.Size = new System.Drawing.Size(153, 21);
            this.textBoxSCA.TabIndex = 2;
            this.textBoxSCA.Text = "+8613800100500";
            // 
            // rbCM
            // 
            this.rbCM.AutoSize = true;
            this.rbCM.Checked = true;
            this.rbCM.Location = new System.Drawing.Point(18, 23);
            this.rbCM.Name = "rbCM";
            this.rbCM.Size = new System.Drawing.Size(71, 16);
            this.rbCM.TabIndex = 0;
            this.rbCM.TabStop = true;
            this.rbCM.Text = "中国移动";
            this.rbCM.UseVisualStyleBackColor = true;
            this.rbCM.CheckedChanged += new System.EventHandler(this.rbSCA_CheckedChanged);
            // 
            // groupBox3
            // 
            this.groupBox3.Controls.Add(this.textBoxPhone);
            this.groupBox3.Controls.Add(this.label1);
            this.groupBox3.Location = new System.Drawing.Point(12, 162);
            this.groupBox3.Name = "groupBox3";
            this.groupBox3.Size = new System.Drawing.Size(419, 46);
            this.groupBox3.TabIndex = 2;
            this.groupBox3.TabStop = false;
            // 
            // textBoxPhone
            // 
            this.textBoxPhone.Location = new System.Drawing.Point(87, 14);
            this.textBoxPhone.Name = "textBoxPhone";
            this.textBoxPhone.Size = new System.Drawing.Size(216, 21);
            this.textBoxPhone.TabIndex = 1;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(16, 18);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(65, 12);
            this.label1.TabIndex = 0;
            this.label1.Text = "手机号码:";
            // 
            // groupBox4
            // 
            this.groupBox4.Controls.Add(this.textBoxMessage);
            this.groupBox4.Location = new System.Drawing.Point(12, 225);
            this.groupBox4.Name = "groupBox4";
            this.groupBox4.Size = new System.Drawing.Size(420, 174);
            this.groupBox4.TabIndex = 3;
            this.groupBox4.TabStop = false;
            this.groupBox4.Text = "短信内容";
            // 
            // textBoxMessage
            // 
            this.textBoxMessage.Location = new System.Drawing.Point(18, 23);
            this.textBoxMessage.Multiline = true;
            this.textBoxMessage.Name = "textBoxMessage";
            this.textBoxMessage.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.textBoxMessage.Size = new System.Drawing.Size(383, 137);
            this.textBoxMessage.TabIndex = 0;
            // 
            // groupBox5
            // 
            this.groupBox5.Controls.Add(this.textBoxPDU);
            this.groupBox5.Location = new System.Drawing.Point(12, 409);
            this.groupBox5.Name = "groupBox5";
            this.groupBox5.Size = new System.Drawing.Size(420, 201);
            this.groupBox5.TabIndex = 4;
            this.groupBox5.TabStop = false;
            this.groupBox5.Text = "PDU数据包";
            // 
            // textBoxPDU
            // 
            this.textBoxPDU.Location = new System.Drawing.Point(18, 23);
            this.textBoxPDU.Multiline = true;
            this.textBoxPDU.Name = "textBoxPDU";
            this.textBoxPDU.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.textBoxPDU.Size = new System.Drawing.Size(383, 164);
            this.textBoxPDU.TabIndex = 0;
            // 
            // buttonClear
            // 
            this.buttonClear.Location = new System.Drawing.Point(30, 624);
            this.buttonClear.Name = "buttonClear";
            this.buttonClear.Size = new System.Drawing.Size(82, 36);
            this.buttonClear.TabIndex = 1;
            this.buttonClear.Text = "清空";
            this.buttonClear.UseVisualStyleBackColor = true;
            this.buttonClear.Click += new System.EventHandler(this.buttonClear_Click);
            // 
            // buttonEncode
            // 
            this.buttonEncode.Location = new System.Drawing.Point(180, 624);
            this.buttonEncode.Name = "buttonEncode";
            this.buttonEncode.Size = new System.Drawing.Size(82, 36);
            this.buttonEncode.TabIndex = 5;
            this.buttonEncode.Text = "短信编码";
            this.buttonEncode.UseVisualStyleBackColor = true;
            this.buttonEncode.Click += new System.EventHandler(this.buttonEncode_Click);
            // 
            // buttonDecode
            // 
            this.buttonDecode.Location = new System.Drawing.Point(330, 624);
            this.buttonDecode.Name = "buttonDecode";
            this.buttonDecode.Size = new System.Drawing.Size(82, 36);
            this.buttonDecode.TabIndex = 7;
            this.buttonDecode.Text = "短信解码";
            this.buttonDecode.UseVisualStyleBackColor = true;
            this.buttonDecode.Click += new System.EventHandler(this.buttonDecode_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(443, 673);
            this.Controls.Add(this.buttonDecode);
            this.Controls.Add(this.buttonClear);
            this.Controls.Add(this.buttonEncode);
            this.Controls.Add(this.groupBox5);
            this.Controls.Add(this.groupBox3);
            this.Controls.Add(this.groupBox2);
            this.Controls.Add(this.groupBox1);
            this.Controls.Add(this.groupBox4);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MaximizeBox = false;
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "短信编解码器 V1.0 By 秦建辉";
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.groupBox2.ResumeLayout(false);
            this.groupBox2.PerformLayout();
            this.groupBox3.ResumeLayout(false);
            this.groupBox3.PerformLayout();
            this.groupBox4.ResumeLayout(false);
            this.groupBox4.PerformLayout();
            this.groupBox5.ResumeLayout(false);
            this.groupBox5.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.RadioButton rbAuto;
        private System.Windows.Forms.RadioButton rbUCS2;
        private System.Windows.Forms.RadioButton rbASCII;
        private System.Windows.Forms.RadioButton rbGSM;
        private System.Windows.Forms.GroupBox groupBox2;
        private System.Windows.Forms.RadioButton rbCU;
        private System.Windows.Forms.TextBox textBoxSCA;
        private System.Windows.Forms.RadioButton rbCM;
        private System.Windows.Forms.GroupBox groupBox3;
        private System.Windows.Forms.TextBox textBoxPhone;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.GroupBox groupBox4;
        private System.Windows.Forms.TextBox textBoxMessage;
        private System.Windows.Forms.GroupBox groupBox5;
        private System.Windows.Forms.TextBox textBoxPDU;
        private System.Windows.Forms.Button buttonClear;
        private System.Windows.Forms.Button buttonEncode;
        private System.Windows.Forms.Button buttonDecode;
    }
}

Comments are closed.