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:在連接字符加入字符編碼聲明html
<add key="mysqlconstr" value="UserId=root;Allow Zero Datetime=true;Charset=utf8;Host=125.*.*.*;Database=dbname;Password=123456"/>mysql
string connectiontext = "Server=139.222.313.153;Database=testsys;User=root;Password=1123456;Charset=utf8;";sql
向mysql說明個人字符編碼是gb2312 或者 Utf-8, 不要搞錯.數據庫
只要在鏈接MySQL時,正確地設定了字符集,不管數據庫自己是使用什麼格式編碼的,都能獲得正確的結果。也許有人會覺得寫數據時設定的字符集必需和讀數據時一致,事實上徹底沒有必要。程序所要作的只是告訴 MySQL,目前操做MySQL使用的是什麼字符集便可。由於MySQL會自動完成以下的轉換工做:
寫數據庫時用的字符集-->存諸數據的字符集-->讀取數據的字符集。post