閱讀182 返回首頁    go 阿裏雲 go 技術社區[雲棲]


關於數據編碼的問題(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;
        }

 

 

最後更新:2017-04-02 06:52:25

  上一篇:go linq to xml
  下一篇:go GridView CSS的完整樣式表