C#将字符转换成utf8编码 GB321编码转换

 
复制代码
public static    string  get_uft8(string unicodeString)
{
UTF8Encoding utf8 = new UTF8Encoding();
Byte[] encodedBytes = utf8.GetBytes(unicodeString);
String decodedString = utf8.GetString(encodedBytes);
return decodedString;
}
复制代码

 

复制代码
   这边我以big5转换gb2312为例
Encoding big5 =Encoding.GetEncoding("big5");
Encoding gb2312 = Encoding.GetEncoding("gb2312");


byte[] big5b= big5.GetBytes("編程無悔!");
//关键也就是这句了
byte[] gb2312b= Encoding.Convert(big5,gb2312,big5b);

string strGb2312 = gb2312.GetString(gb2312b)
复制代码

 

1:在链接字符加入字符编码声明

<add key="mysqlconstr" value="UserId=root;Allow Zero Datetime=true;Charset=utf8;Host=125.*.*.*;Database=dbname;Password=123456"/>

 string connectiontext = "Server=139.222.313.153;Database=testsys;User=root;Password=1123456;Charset=utf8;";

向mysql说明我的字符编码是gb2312 或者 Utf-8, 不要搞错.

只要在连接MySQL时,正确地设定了字符集,无论数据库本身是使用什么格式编码的,都能得到正确的结果。也许有人会以为写数据时设定的字符集必需和读数据时一致,事实上完全没有必要。程序所要做的只是告诉 MySQL,目前操作MySQL使用的是什么字符集即可。因为MySQL会自动完成如下的转换工作:

写数据库时用的字符集-->存诸数据的字符集-->读取数据的字符集。

 

posted on 2017-01-18 21:57  王庆东mas  阅读(2924)  评论(0编辑  收藏  举报