编码格式的转换
2010-04-25 21:33
218 查看
(整理:网站建设与网站制作公司-VeiSun.Com)c#实现GB2312和UTF8字符编码方式的转换
public string UTF8ToGB2312(string str)
{
try
{
Encoding utf8 = Encoding.GetEncoding(65001);
Encoding gb2312 = Encoding.GetEncoding("gb2312");//Encoding.Default ,936
byte[] temp = utf8.GetBytes(str);
byte[] temp1 = Encoding.Convert(utf8, gb2312, temp);
string result = gb2312.GetString(temp1);
return result;
}
catch (Exception ex)//(UnsupportedEncodingException ex)
{
MessageBox.Show(ex.ToString());
return null;
}
}
public string GB2312ToUTF8(string str)
{
try
{
Encoding uft8 = Encoding.GetEncoding(65001);
Encoding gb2312 = Encoding.GetEncoding("gb2312");
byte[] temp = gb2312.GetBytes(str);
MessageBox.Show("gb2312的编码的字节个数:" + temp.Length);
for (int i = 0; i < temp.Length; i++)
{
MessageBox.Show(Convert.ToUInt16(temp[i]).ToString());
}
byte[] temp1 = Encoding.Convert(gb2312, uft8, temp);
MessageBox.Show("uft8的编码的字节个数:" + temp1.Length);
for (int i = 0; i < temp1.Length; i++)
{
MessageBox.Show(Convert.ToUInt16(temp1[i]).ToString());
}
string result = uft8.GetString(temp1);
return result;
}
catch (Exception ex)//(UnsupportedEncodingException ex)
{
MessageBox.Show(ex.ToString());
return null;
}
}
代码说明:
Encoding utf8 = Encoding.GetEncoding(65001);//使用code page
Encoding gb2312 = Encoding.GetEncoding("gb2312");//通过bodyname
获取字符编码字节序列:byte[] temp=utf8.GetBytes(str);
编码方式转换:byte[] temp1=Encoding.Convert(utf8, gb2312, temp);
获取编码的字符串:string str1=gb2312.GetString(temp1);
这样即完成了字符编码的转换。
Encoding.Default在 简体中文os中一般是gb2312格式。
public string UTF8ToGB2312(string str)
{
try
{
Encoding utf8 = Encoding.GetEncoding(65001);
Encoding gb2312 = Encoding.GetEncoding("gb2312");//Encoding.Default ,936
byte[] temp = utf8.GetBytes(str);
byte[] temp1 = Encoding.Convert(utf8, gb2312, temp);
string result = gb2312.GetString(temp1);
return result;
}
catch (Exception ex)//(UnsupportedEncodingException ex)
{
MessageBox.Show(ex.ToString());
return null;
}
}
public string GB2312ToUTF8(string str)
{
try
{
Encoding uft8 = Encoding.GetEncoding(65001);
Encoding gb2312 = Encoding.GetEncoding("gb2312");
byte[] temp = gb2312.GetBytes(str);
MessageBox.Show("gb2312的编码的字节个数:" + temp.Length);
for (int i = 0; i < temp.Length; i++)
{
MessageBox.Show(Convert.ToUInt16(temp[i]).ToString());
}
byte[] temp1 = Encoding.Convert(gb2312, uft8, temp);
MessageBox.Show("uft8的编码的字节个数:" + temp1.Length);
for (int i = 0; i < temp1.Length; i++)
{
MessageBox.Show(Convert.ToUInt16(temp1[i]).ToString());
}
string result = uft8.GetString(temp1);
return result;
}
catch (Exception ex)//(UnsupportedEncodingException ex)
{
MessageBox.Show(ex.ToString());
return null;
}
}
代码说明:
Encoding utf8 = Encoding.GetEncoding(65001);//使用code page
Encoding gb2312 = Encoding.GetEncoding("gb2312");//通过bodyname
获取字符编码字节序列:byte[] temp=utf8.GetBytes(str);
编码方式转换:byte[] temp1=Encoding.Convert(utf8, gb2312, temp);
获取编码的字符串:string str1=gb2312.GetString(temp1);
这样即完成了字符编码的转换。
Encoding.Default在 简体中文os中一般是gb2312格式。
相关文章推荐
- js将echarts图表转换成base64位格式编码的图片
- 递归转换数组或字符串的编码格式
- 多种国际编码格式之间进行文本内码的转换
- Swift - UIColor16进制编码与RGB格式互相转换
- 将普通字符串转换成MD5编码格式字符
- javaCV开发详解之7:让音频转换更加简单,实现通用音频编码格式转换、重采样等音频参数的转换功能(以pcm16le编码的wav转mp3为例)
- Python中unicode编码的字符串和其他格式的字符串之间进行转换
- ANSI、UNICODE、UTF-8编码格式相互之间转换
- c++ 解析从浏览器端传过来的图像base64编码,并转换成opencv识别的格式
- 编码格式互相转换顺序和次数
- 识别常见编码格式文件并转换成UTF-8编码的java实现
- oracle编码格式从utf-8转换为GBK
- 文件编码格式转换
- NSString值为Unicode格式的字符串编码转换成中文编码
- Linux查看文件编码格式及文件编码转换
- Python中文编码格式转换——usc2转ansi
- 用C#写的小工具,可以将当前文件夹及其子文件夹内的所有文本文件转换为utf-8编码格式。
- UTF-8和GBK等中文字符编码格式介绍及相互转换
- linux下如何查看文件编码格式及转换文件编码
- linux系统下文件编码格式的转换