Converting UTF-8 to ISO-8859-1 in Java

后端 未结 4 1509
挽巷
挽巷 2020-12-08 11:45

I am reading an XML document (UTF-8) and ultimately displaying the content on a Web page using ISO-8859-1. As expected, there are a few characters are not displayed correctl

4条回答
  •  青春惊慌失措
    2020-12-08 12:14

    Depending on your default encoding, following lines could cause problem,

    byte[] latin1 = sb.toString().getBytes("ISO-8859-1");
    
    return new String(latin1);
    

    In Java, String/Char is always in UTF-16BE. Different encoding is only involved when you convert the characters to bytes. Say your default encoding is UTF-8, the latin1 buffer is treated as UTF-8 and some sequence of Latin-1 may form invalid UTF-8 sequence and you will get ?.

提交回复
热议问题