关于数据编码的问题(utf-8 to gbk)

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;
        }

 

 

上一篇:维基解密披露CIA Grasshopper远程木马套件 Windows预安装环境、Carberp财务恶意软件的计算机驻留方法都用上了


下一篇:给在生产环境下给php安装apc加速扩展脚本