Java, Using Scanner to input characters as UTF-8, can't print text

后端 未结 2 1781
萌比男神i
萌比男神i 2021-01-19 21:49

I can convert String to Array as UTF-8, but I can\'t convert it back to String like the first String.

public static void main(String[] args) {

    Scanner          


        
2条回答
  •  终归单人心
    2021-01-19 22:29

    String s = new String(theByteArray);
    

    should really be

    String s = new String(theByteArray, Charset.forName("UTF-8"));
    

    The underlying issue here is that String constructors aren't smart. The String constructor cannot distinguish the charset that is being used and will try to convert it using the system standard which is generally something like ASCII or ISO-8859-1. This is why normal A-Za-z looks proper but then everything else begins to fail.

    byte is a type that runs from -127 to 127 thus for UTF-8 conversion consecutive bytes need to be concatenated. It's impossible for the String constructor to distinguish this off a byte array so it will handle each byte individually by default (thus why basic alphanumeric will always work as they fall into this range).

    Example:

    String text = "こんにちは";
    byte[] array = text.getBytes("UTF-8");
    String s = new String(array, Charset.forName("UTF-8"));
    System.out.println(s); // Prints as expected
    String sISO = new String(array, Charset.forName("ISO-8859-1")); // Prints 'ããã«ã¡ã¯'
    System.out.println(sISO);
    

提交回复
热议问题