当前位置:首页 > C# > 正文内容

c#汉字与编码之间的转换(输出十六进制)

admin9年前 (2015-12-02)C#4955
/******************************************************************/
        /***********************               ****************************/
        /*********************** 汉字转换工具  ****************************/
        /***********************               ****************************/
        /******************************************************************/


        /****************************  字符串转编码函数 **********************************/
        private byte[] StringToBytes(string TheString)
        {
            Encoding encoding = Encoding.GetEncoding("UTF-8");
            Encoding encoding2 = Encoding.GetEncoding("gb2312");
            byte[] bytes = encoding.GetBytes(TheString);
            return Encoding.Convert(encoding, encoding2, bytes);
        }
        /****************************  编码转字符串函数 **********************************/
        private string BytesToString(byte[] Bytes)
        {
            Encoding encoding = Encoding.GetEncoding("gb2312");
            Encoding encoding2 = Encoding.GetEncoding("UTF-8");
            byte[] bytes = Encoding.Convert(encoding, encoding2, Bytes);
            return encoding2.GetString(bytes);
        }
        /****************************  单击转换按钮事件 **********************************/
        private void Changez_Click(object sender, EventArgs e)
        {
            if (this.CHcode.Checked)//判断什么类型的转换
            {
                byte[] array = this.StringToBytes(this.intextz.Text);
                this.outtextz.Text = "";
                byte[] array2 = array;
                for (int i = 0; i < array2.Length; i++)
                {
                    byte b = array2[i];
                    string text = b.ToString("x").ToUpper();
                    TextBox expr_64 = this.outtextz;
                    expr_64.Text = expr_64.Text + "0x" + ((text.Length == 1) ? ("0" + text) : text) + " ";
                }
            }
            else
            {
                if (!this.CHcode.Checked)
                {
                    byte[] array3 = new byte[this.intextz.Text.Length / 2];
                    try
                    {
                        string text2 = this.intextz.Text;
                        text2 = text2.Replace("0x", "");
                        text2 = text2.Replace(" ", string.Empty);
                        for (int j = 0; j < text2.Length / 2; j++)
                        {
                            array3[j] = Convert.ToByte(text2.Substring(j * 2, 2), 16);
                        }
                        this.outtextz.Text = this.BytesToString(array3);
                    }
                    catch
                    {
                        MessageBox.Show("数据转换错误,请输入数字。", "错误");
                    }
                }
            }
        }


扫描二维码推送至手机访问。

版权声明:本文由视觉博客发布,如需转载请注明出处。

本文链接:https://feelsight.cn/post/6.html

“c#汉字与编码之间的转换(输出十六进制)” 的相关文章

基于C#的socket编程的TCP异步实现

基于C#的socket编程的TCP异步实现

一、摘要  本篇博文阐述基于TCP通信协议的异步实现。 二、实验平台  Visual Studio 2010 三、异步通信实现原理及常用方法3.1 建立连接   在同步模式中,在服务器上使用Accept方法接入连接请求,而在客户端则使用Connect方法来连接服务器。相对地,在异...

安装包制作工具 SetupFactory使用2 API清单

安装包制作工具 SetupFactory使用2 API清单

SetupFactory中可以通过其API控制很复杂的业务需求。   下图中展示了其内置的API种类与具体分类函数。             ...

C#中的MessageBox消息对话框

C#中的MessageBox消息对话框

在程序中,我们经常使用消息对话框给用户一定的信息提示,如在操作过程中遇到错误或程序异常,经常会使用这种方式给用于以提示。在C#中,MessageBox消息对话框位于System.Windows.Forms命名空间中,一般情况,一个消息对话框包含信息提示文字内容、消息对话框的标题文字、用户响应的...

C#实现邮件发送的功能

很多时候需要邮件发送功能,例如:监测应用,需要上报状态。   微软封装好的MailMessage类:主要处理发送邮件的内容(如:收发人地址、标题、主体、图片等等)   微软封装好的SmtpClient类:主要处理用smt...

c# 全局鼠标事件

1.Win32Api public class Win32Api { [StructLayout(LayoutKind.Sequential)] public class POINT { publi...

C#全局监听Windows键盘事件

1.工具类代码 using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using System.Windows...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。