中文乱码 mysql数据库latin1 乱码 latin1 转 UTF-8

同步数据库 ,从A数据库(编码为latin1)取出数据,数据为  刘广州 ,传到B数据库(编码为UTF8),同步过程中 java 对 刘广州 数据转成UTF8,以让其中文正常显示。

Mysql 的latin1 不等于标准的latin1(iso-8859-1) 和cp1252,比iso-8859-1多了0x80-0x9f字符,比cp1252多了0x81,0x8d,0x8f,0x90,0x9d 一共5个字符。

这样在Java中,如果使用标准的iso-8859-1或者cp1252解码可能出现乱码。
s.getBytes("iso-8859-1") 或者 s.getBytes("cp1252"); 所以用以下方法解决。


public String convertCharset(String s)
    {
        if (s != null)
        {
            try
            {
                int length = s.length();
                byte[] buffer = new byte[length];
                //0x81 to Unicode 0x0081, 0x8d to 0x008d, 0x8f to 0x008f, 0x90 to 0x0090, and 0x9d to 0x009d.
                for (int i = 0; i < length; ++i)
                {
                    char c = s.charAt(i);
                    if (c == 0x0081)
                    {
                        buffer[i] = (byte) 0x81;
                    }
                    else if (c == 0x008d)
                    {
                        buffer[i] = (byte) 0x8d;
                    }
                    else if (c == 0x008f)
                    {
                        buffer[i] = (byte) 0x8f;
                    }
                    else if (c == 0x0090)
                    {
                        buffer[i] = (byte) 0x90;
                    }
                    else if (c == 0x009d)
                    {
                        buffer[i] = (byte) 0x9d;
                    }
                    else
                    {
                        buffer[i] = Character.toString(c).getBytes("CP1252")[0];
                    }
                }
                String result = new String(buffer, "UTF-8");
                return result;
            }
            catch (UnsupportedEncodingException e)
            {
                e.printStackTrace();
            }
        }
        return null;
    }

http://dev.mysql.com/doc/refman/5.0/en/charset-we-sets.html
http://www.oschina.net/question/2795_74288

猜你喜欢

转载自qi20088.iteye.com/blog/2240351
今日推荐