简体   繁体   中英

How Java Convert latin to UTF-8 perfect?

When I try to convert latin1 String to utf8 by Java,something wrong happen. as follows code:

    byte[] latin2 = "¦ñ¨ãÓñ²½ìá".getBytes("ISO-8859-1");
    byte[] latin1 = "¦á¨ãÓñ²½ìá".getBytes("ISO-8859-1");
    byte[] utf8 = new String(latin1, "GB2312").getBytes("GB2312");
    byte[] utf81 = new String(latin2, "GB2312").getBytes("GB2312");
    System.out.println(new String(utf8,"GB2312"));
    System.out.println(new String(utf81,"GB2312"));

The output is

 ?ㄣ玉步灬
 ?ㄣ玉步灬

So,I'm comfused about it,how can i convert latin1 to utf8 exact?

The DB field is:

`name` char(20) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL,

新的String(bytes,charset)调用中的第二个参数是设置用于解码字节数组的字符集(来自Javadoc:“ charset用于解码字节的字符集”)...因此,在您的情况下,应该设置为用于编码字节的编码 :“ ISO-8859-1”:

new String(latin1, "ISO-8859-1").getBytes("GB2312");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM