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

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

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


        /****************************  字符串转编码函数 **********************************/
        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("数据转换错误,请输入数字。", "错误");
                    }
                }
            }
        }


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

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

本文链接:http://cqroom.cn/post/6.html

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

C#中的MessageBox消息对话框

C#中的MessageBox消息对话框

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

C# 窗体间传值(Form与From之间互相传值)

C# 窗体间传值(Form与From之间互相传值)

1、委托   两个窗体,窗体很简单,只实现改变颜色功能,一看就会: 代码如下,只贴按钮事件代码: 打开Form2按钮事件 private void button1_Click(object s...

高恪路由器批量管理监控系统

高恪路由器批量管理监控系统

一、采用C# 编写,可以批量监控路由器是否正常 功能: 1.显示当前路由IP、名字、实时上行速度、实时下行速度、主机数量、连接数、CPU占用率、内存占用率、CPU温度、运行时间等 2.可以显示10分钟实时流量 3.可以显示历史2小时、...

c#程序闪退日志记录/异常日志

以下代码是程序入口文件 using DDS_Form1; using System; using System.Collections.Generic; using System.IO; //using System.Linq; using System.Windows.Forms;...

C# .net动态加载第三方DLL

C#动态加载第三方DLL 当我们需要加载第三方非托管DLL时,通常会直接使用DllImport的方式,代码如下: [DllImport("GetFile.dll", CallingConvention = CallingConvention.StdCall,...

C#调用c++的dll执行带参数的函数时 请检查 PInvoke 签名的调用约定和参数与非托管的目标签名是否匹配

C#动态调用DLL 由于Dll路径的限制,使用的不是很方便,C#中我们经常通过配置动态的调用托管Dll,例如常用的一些设计模式:Abstract Factory, Provider, Strategy模式等等,那么是不是也可以这样动态调用C++动态链接呢?只要您还记得在C++中,通...

发表评论

访客

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