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

本文介绍了一个用于汉字转换的工具,提供了字符串与编码之间的相互转换功能。支持从UTF-8到GB2312及反向转换,适用于需要进行字符集转换的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 /******************************************************************/
        /***********************               ****************************/
        /*********************** 汉字转换工具  ****************************/
        /***********************               ****************************/
        /******************************************************************/


        /****************************  字符串转编码函数 **********************************/
        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://www.cnblogs.com/mrqiang/p/4348748.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值