一聚教程网:一个值得你收藏的教程网站

热门教程

asp.net C# Encoder and Decoder 网络数据编码与解码

时间:2022-06-25 08:23:47 编辑:袖梨 来源:一聚教程网

 asp.net在网络通信中,很多情况下通信双方传达的都是字符信息。但是,字符信息并不能直接从网络的一端传递到另一端,这些字符信息首先需要被转换成一个字节序列后才能在网络中传输。将字符序列转换为字节序列的过程称为编码。当这些字节传送到网络的接收方时,接收方需要反过来将字节序列再转换为字符序列,这种过程称为解码。

下面是编码与解码的例子:

截图:

  proame.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

 代码如下 复制代码
namespace EncoderDecoderExample
{
    static class Program
    {
        ///
        /// 应用程序的主入口点。
        ///

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

form1.cs

 代码如下 复制代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EncoderDecoderExample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            txt_EncodeStart.Text = "这是一条测试数据:abc,123ABC..。。n test string";
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //显示现有的编码类型
            foreach (EncodingInfo ei in Encoding.GetEncodings())
            {
                Encoding en = ei.GetEncoding();
                cbo_EncodeType.Items.Add(string.Format("{0}[{1}]", en.HeaderName, en.EncodingName));
            }
            cbo_EncodeType.SelectedIndex = cbo_EncodeType.FindString("gb2312");
        }

        private void btn_EncodeAndDecode_Click(object sender, EventArgs e)
        {
            //编码
            string codeType = this.cbo_EncodeType.SelectedItem.ToString();
            codeType = codeType.Substring(0, codeType.IndexOf('['));        //获得编码类型 gb2312
            Encoder encoder = Encoding.GetEncoding(codeType).GetEncoder();  //获得一个 gb2312 编码类型的编码器
            char[] chars = this.txt_EncodeStart.Text.ToCharArray();         //将字符串转换为一组char数组
            byte[] bytes = new byte[encoder.GetByteCount(chars, 0, chars.Length, true)];    //声明一个长度为‘编码为byte后产生的字节数’
            encoder.GetBytes(chars, 0, chars.Length, bytes, 0, true);       //进行编码,将chars数组中的字符编码到byte数组中
            txt_EncodeOver.Text = Convert.ToBase64String(bytes);            //将 8 位无符号整数数组的值转换为其用 Base64 数字编码的等效字符串 显示到控件中。

            //解码
            Decoder decoder = Encoding.GetEncoding(codeType).GetDecoder();      //获得编码类型为 gb2312 的解码器
            int charLen = decoder.GetChars(bytes, 0, bytes.Length, chars, 0);   //进行解码,将byte数组中的8位无符号整数转换为 char字符
            String strResult = "";
            foreach (char c in chars)
                strResult += c.ToString(); 
            txt_DecodeOver.Text = strResult;

        }


    }
}

form1.desgi.cs

 代码如下 复制代码

namespace EncoderDecoderExample
{
    partial class Form1
    {
        ///


        /// 必需的设计器变量。
        ///

        private System.ComponentModel.IContainer components = null;

        ///


        /// 清理所有正在使用的资源。
        ///

        /// 如果应释放托管资源,为 true;否则为 false。
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

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

        ///


        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        ///

        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.cbo_EncodeType = new System.Windows.Forms.ComboBox();
            this.label2 = new System.Windows.Forms.Label();
            this.txt_EncodeStart = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.txt_EncodeOver = new System.Windows.Forms.TextBox();
            this.txt_DecodeOver = new System.Windows.Forms.TextBox();
            this.btn_EncodeAndDecode = new System.Windows.Forms.Button();
            this.SuspendLayout();
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(14, 26);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(65, 12);
            this.label1.TabIndex = 0;
            this.label1.Text = "编码类型:";
            //
            // cbo_EncodeType
            //
            this.cbo_EncodeType.FormattingEnabled = true;
            this.cbo_EncodeType.Location = new System.Drawing.Point(85, 23);
            this.cbo_EncodeType.Name = "cbo_EncodeType";
            this.cbo_EncodeType.Size = new System.Drawing.Size(216, 20);
            this.cbo_EncodeType.TabIndex = 1;
            //
            // label2
            //
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(26, 65);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(53, 12);
            this.label2.TabIndex = 2;
            this.label2.Text = "编码前:";
            //
            // txt_EncodeStart
            //
            this.txt_EncodeStart.Location = new System.Drawing.Point(85, 62);
            this.txt_EncodeStart.Multiline = true;
            this.txt_EncodeStart.Name = "txt_EncodeStart";
            this.txt_EncodeStart.Size = new System.Drawing.Size(216, 53);
            this.txt_EncodeStart.TabIndex = 3;
            //
            // label3
            //
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(26, 135);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(53, 12);
            this.label3.TabIndex = 4;
            this.label3.Text = "编码后:";
            //
            // label4
            //
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(26, 204);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(53, 12);
            this.label4.TabIndex = 5;
            this.label4.Text = "解码后:";
            //
            // txt_EncodeOver
            //
            this.txt_EncodeOver.Location = new System.Drawing.Point(85, 132);
            this.txt_EncodeOver.Multiline = true;
            this.txt_EncodeOver.Name = "txt_EncodeOver";
            this.txt_EncodeOver.Size = new System.Drawing.Size(216, 53);
            this.txt_EncodeOver.TabIndex = 6;
            //
            // txt_DecodeOver
            //
            this.txt_DecodeOver.Location = new System.Drawing.Point(85, 204);
            this.txt_DecodeOver.Multiline = true;
            this.txt_DecodeOver.Name = "txt_DecodeOver";
            this.txt_DecodeOver.Size = new System.Drawing.Size(216, 53);
            this.txt_DecodeOver.TabIndex = 7;
            //
            // btn_EncodeAndDecode
            //
            this.btn_EncodeAndDecode.Location = new System.Drawing.Point(125, 268);
            this.btn_EncodeAndDecode.Name = "btn_EncodeAndDecode";
            this.btn_EncodeAndDecode.Size = new System.Drawing.Size(75, 23);
            this.btn_EncodeAndDecode.TabIndex = 8;
            this.btn_EncodeAndDecode.Text = "编码/解码";
            this.btn_EncodeAndDecode.UseVisualStyleBackColor = true;
            this.btn_EncodeAndDecode.Click += new System.EventHandler(this.btn_EncodeAndDecode_Click);
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(329, 303);
            this.Controls.Add(this.btn_EncodeAndDecode);
            this.Controls.Add(this.txt_DecodeOver);
            this.Controls.Add(this.txt_EncodeOver);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.txt_EncodeStart);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.cbo_EncodeType);
            this.Controls.Add(this.label1);
            this.Name = "Form1";
            this.Text = "编码/解码";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.ComboBox cbo_EncodeType;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.TextBox txt_EncodeStart;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.TextBox txt_EncodeOver;
        private System.Windows.Forms.TextBox txt_DecodeOver;
        private System.Windows.Forms.Button btn_EncodeAndDecode;
    }
}

源码下载地址:http://www.111com.net/down/EncoderDecoderExample.rar

热门栏目