(转自:http://blog.sina.com.cn/s/blog_498eab7d0100et7j.html)

根据查找的System.Text.Encoding类的属性,方法写了如下的转换程序:

         public string UTF8ToGB2312(string str)
...{
try
...{
Encoding utf8 = Encoding.GetEncoding();
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();
Encoding gb2312 = Encoding.GetEncoding("gb2312");
byte[] temp = gb2312.GetBytes(str);
MessageBox.Show("gb2312的编码的字节个数:" + temp.Length);
for (int i = ; 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 = ; 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格式

网上流传的第二种方法:

在使用MySql时会遇到中文乱码的问题就此写下面两个函数

*   
在写入数据库和从数据库读出时将编码改变  
        
*   
author:alice  
        
*   
date       
:2006/1/25  
     
*/  
     
//写入数据库时进行转换

  public   string    GB2312_ISO8859(string    write)
{
//声明字符集
System.Text.Encoding iso8859, gb2312;
//iso8859
iso8859 = System.Text.Encoding.GetEncoding("iso8859-1");
//国标2312
gb2312 = System.Text.Encoding.GetEncoding("gb2312");
byte[] gb;
gb = gb2312.GetBytes(write);
//返回转换后的字符
return iso8859.GetString(gb);
} //读出时进行转换
public string ISO8859_GB2312(string read)
{
//声明字符集
System.Text.Encoding iso8859,gb2312;
//iso8859
iso8859 = System.Text.Encoding.GetEncoding("iso8859-1");
//国标2312
gb2312 = System.Text.Encoding.GetEncoding("gb2312");
byte[] iso;
iso = iso8859.GetBytes(read);
//返回转换后的字符
return gb2312.GetString(iso);
}

C#中的字符串及其编码转换的更多相关文章

  1. 中文字符串的编码转换(c实现)

    中文字符串在c/c++中表示为字节序列,在分词的时候需要根据不同的编码方式进行分词,一般分词器需要转换成统一的编码方式再进行转换,有些分词器如ICTCLAS在分词的时候可以不显示定义编码方式,可以检测 ...

  2. ASP中Utf-8与Gb2312编码转换乱码问题的解决方法 页面编码声明

    ASP程序在同一个站点中,如果有UTF-8编码的程序,又有GB2312编码的程序时,在浏览UTF-8编码的页面后,再浏览当前网站GB2312的页面,GB2312编码的页面就会出现乱码 出现这样的问题是 ...

  3. 字符串js编码转换成实体html编码的方法(防范XSS攻击)

    js代码在html页面中转换成实体html编码的方法一: <!DOCTYPE html><html> <head>    <title>js代码转换成实 ...

  4. python中的字符串和编码

    了解编码之前首先说下这几个词的概率: 位.字节.字符.字符串 1.位(bit)也称为比特 这个其实很简单,因为计算机都是二进制存储数据,也就是0和1,一个0或者1就表示一位.这是计算机存储的最小单位. ...

  5. python中http的一些编码转换

    http的数据需要2种编码解码. 1. url中的特殊字符转换, 比如",', :,//等 python3中通过urllib.parse.quote(..)和urllib.parse.unq ...

  6. 002、Python中json字符串与字典转换

    1.测试用例文件TestCase.xlsx 2.编写Python文件进行读取 #!/usr/bin/env python # -*- coding:utf-8 -*- import time impo ...

  7. python2.7与3.5版本中:编码格式及编码转换

    主要说明编码之间的转换方法 2.7版本: 1 # -*- coding:utf-8 -*- 2 a = "迪丽热巴" 3 a_unicode = a.decode("ut ...

  8. 在C语言中使用libiconv进行编码转换的示例

    libiconv_sample.c #include <stdio.h> #include <malloc.h> #include "libiconv/iconv.h ...

  9. JAVA字符串编码转换常用类

    无论是对程序的本地化还是国际化,都会涉及到字符编码的转换的问题.尤其在web应用中常常需要处理中文字符,这时就需要进行字符串的编码转换,将字符串编码转换为GBK或者GB2312.一.关键技术点:    ...

随机推荐

  1. Python__return

    return的注意点: return返回的值, 没有任何类型限制 return是函数结束的标志,函数内可以写多个return,但执行一次,函数就立刻结束,并把return后的值作为本次调用的返回值.

  2. mybatis分享

    Mybatis入门 一.Mybatis环境搭建及简单实例 pom.xml mybatis-config.xml <?xml version="1.0" encoding=&q ...

  3. Centos学习笔记2-网络部分

    一:修改IP地址:vi /etc/sysconfig/network-scripts/ifcfg-eth0 IPADDR=192.168.80.100 NETMASK=255.255.255.0 GA ...

  4. LeetCode:组合总数II【40】

    LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...

  5. mysql数据库存储过程数据迁移案例与比较

    cursor 与 insert ...select 对比: cursor:安全,不会造成死锁,可以在服务运行阶段跑,比较稳定. insert...select :速度快,但是可能造成死锁,相比curs ...

  6. PAT 天梯赛 L1-047. 装睡 【水】

    题目链接 https://www.patest.cn/contests/gplt/L1-047 AC代码 #include <iostream> #include <cstdio&g ...

  7. JavaScript返回顶部特效

    样式: <style type="text/css"> /*返回顶部特效*/ a { border: none; text-decoration: none; outl ...

  8. python的垃圾回收机制 继承的顺序C3算法

    Python垃圾回收    -- 引用计数        -- Python为每个对象维护一个引用计数        -- 当引用计数为0的 代表这个对象为垃圾    -- 标记清除        - ...

  9. Zabbix Windos agent 安装

    系统:Windos 2008 R2 x64 服务:Zabbix_agents_3.0.4.win 一.安装Zabbix_agents_3.0.4.win 1.下载Zabbix_agents_3.0.4 ...

  10. Apache 工作模式配置优化

    Apahce 工作模式配置 1.查看当前MPM工作模式 /usr/local/apache2/bin/apachectl -V Server version: Apache/2.4.27 (Unix) ...