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會自動完成如下的轉換工作:
寫數據庫時用的字符集-->存諸數據的字符集-->讀取數據的字符集。

