代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
汉字与GBK,Unicode,UTF-8编码之间的转换
作者:
greatverve
/ 发布于
2012/10/25
/
740
汉字与GBK,Unicode,UTF-8编码之间的转换
<div>private void button1_Click(object sender, EventArgs e)</div> <div> {</div> <div> //汉字转为Unicode编码:</div> <div> string hz = textBox1.Text.ToString();</div> <div> byte[] b=Encoding.Unicode.GetBytes(hz);</div> <div> string o = "";</div> <div> foreach(var x in b){</div> <div> o += string.Format("{0:X2}",x) + " ";</div> <div> } <div> textBox2.Text = o;</div> <div> } <div></div> <div>private void button2_Click(object sender, EventArgs e)</div> <div> {</div> <div> //Unicode编码转为汉字:</div> <div> string cd = textBox2.Text.ToString();</div> <div> string cd2 = cd.Replace(" ", "");</div> <div> cd2 = cd2.Replace("\r", "");</div> <div> cd2 = cd2.Replace("\n", "");</div> <div> cd2 = cd2.Replace("\r\n", "");</div> <div> cd2 = cd2.Replace("\t", "");</div> <div> if (cd2.Length % 4 != 0)</div> <div> {</div> <div> MessageBox.Show("Unicode编码为双字节,请删多或补少!确保是二的倍数。");</div> <div> } <div> else</div> <div> {</div> <div> int len = cd2.Length / 2;</div> <div> byte[] b = new byte[len];</div> <div> for (int i = 0; i < cd2.Length;i+=2 )</div> <div> {</div> <div> string bi = cd2.Substring(i, 2);</div> <div> b[i/2] =(byte) Convert.ToInt32(bi, 16);</div> <div> } <div> string o=Encoding.Unicode.GetString(b);</div> <div> textBox1.Text = o;</div> <div> } <div> } <div></div> <div> private void button5_Click(object sender, EventArgs e)</div> <div> {</div> <div> //汉字转成GBK十六进制码:</div> <div> string hz = textBox3.Text.ToString();</div> <div>byte[] gbk = Encoding.GetEncoding("GBK").GetBytes(hz);</div> <div> string s1 = ""; string s1d = "";</div> <div> foreach(byte b in gbk){</div> <div> //s1 += Convert.ToString(b, 16)+" ";</div> <div> s1 += string.Format("{0:X2}", b) + " ";</div> <div> s1d += b + " ";</div> <div> toolTip1.SetToolTip(textBox4, s1d);</div> <div> } <div> textBox4.Text = s1;</div> <div> toolTip1.SetToolTip(textBox4, s1d);</div> <div> //汉字转成Unicode十六进制码:</div> <div> byte[] uc = Encoding.Unicode.GetBytes(hz);</div> <div> string s2 = ""; string s2d = "";</div> <div> foreach (byte b in uc)</div> <div> {</div> <div> //s2 += Convert.ToString(b, 16) + " ";</div> <div> s2 += string.Format("{0:X2}", b) + " ";</div> <div> s2d += b + " ";</div> <div> toolTip1.SetToolTip(textBox5, s2d);</div> <div> } <div> textBox5.Text = s2;</div> <div> toolTip1.SetToolTip(textBox5, s2d);</div> <div> //汉字转成UTF-8十六进制码:</div> <div> byte[] utf8 = Encoding.UTF8.GetBytes(hz);</div> <div> string s3 = ""; string s3d = "";</div> <div> foreach (byte b in utf8)</div> <div> {</div> <div> //s3 += Convert.ToString(b, 16) + " ";</div> <div> s3 += string.Format("{0:X2}", b) + " ";</div> <div> s3d += b + " ";</div> <div> toolTip1.SetToolTip(textBox6, s3d);</div> <div> } <div> textBox6.Text = s3;</div> <div> toolTip1.SetToolTip(textBox6, s3d);</div> <div> } <div></div> <div> private void button6_Click(object sender, EventArgs e)</div> <div> { //GBK十六进制码转成汉字:</div> <div> string cd = textBox4.Text.ToString();</div> <div> string[] b4 = cd.Split(' '); </div> <div> byte[] bs=new byte[2];</div> <div> bs[0] = (byte)Convert.ToByte(b4[0], 16);</div> <div> bs[1] = (byte)Convert.ToByte(b4[1], 16);</div> <div> textBox3.Text =Encoding.GetEncoding("GBK").GetString(bs);</div> <div> } <div></div> <div> private void button7_Click(object sender, EventArgs e)</div> <div> { //Unicode十六进制码转成汉字:</div> <div> string cd = textBox5.Text.ToString();</div> <div> string[] b5 = cd.Split(' ');</div> <div> byte[] bs = new byte[2];</div> <div> bs[0] = (byte)Convert.ToByte(b5[0], 16);</div> <div> bs[1] = (byte)Convert.ToByte(b5[1], 16);</div> <div> textBox3.Text = Encoding.GetEncoding("Unicode").GetString(bs);</div> <div> } <div></div> <div> private void button8_Click(object sender, EventArgs e)</div> <div> { //UTF-8十六进制码转成汉字:</div> <div> string cd = textBox6.Text.ToString();</div> <div> string[] b6 = cd.Split(' ');</div> <div> byte[] bs = new byte[3];</div> <div> bs[0] = (byte)Convert.ToByte(b6[0], 16);</div> <div> bs[1] = (byte)Convert.ToByte(b6[1], 16);</div> <div> bs[2] = (byte)Convert.ToByte(b6[2], 16);</div> <div> textBox3.Text = Encoding.GetEncoding("UTF-8").GetString(bs);</div> <div> }
试试其它关键字
编码之间的转换
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
greatverve
贡献的其它代码
(
2
)
.
汉字与GBK,Unicode,UTF-8编码之间的转换
.
TreeView改变选中节点颜色,失去焦点时选中节点仍突显
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3