mysql 的弊病就是没有办法对中文很好的支持
php+mysql的网站显示都很正常
而在数据库下看的中文都是乱码
编码方式是utf-8
而且如果是正确显示的编码gbk
在C#下如何将utf-8转到gbk呢?
private string utf8ToGbk(string utf8string)
{
byte[] buffer1 = Encoding.Default.GetBytes(utf8string);
byte[] buffer2 = Encoding.Convert(Encoding.UTF8, Encoding.Default, buffer1, 0, buffer1.Length);
string strBuffer = Encoding.Default.GetString(buffer2, 0, buffer2.Length);
return strBuffer;
}
还有一个函数也可以
private string utf8togbk2(string utf8string)
{
// Create two different encodings.
Encoding utf8 = Encoding.UTF8;
Encoding defaultCode = Encoding.Default;
// Convert the string into a byte[].
byte[] utf8Bytes = Encoding.Default.GetBytes(utf8string);
// Perform the conversion from one encoding to the other.
byte[] defaultBytes = Encoding.Convert(utf8, defaultCode, utf8Bytes);
// Convert the new byte[] into a char[] and then into a string.
// This is a slightly different approach to converting to illustrate
// the use of GetCharCount/GetChars.
char[] defaultChars = new char[defaultCode.GetCharCount(defaultBytes, 0, defaultBytes.Length)];
defaultCode.GetChars(defaultBytes, 0, defaultBytes.Length, defaultChars, 0);
string defaultString = new string(defaultChars);
return defaultString;
}