问题
When writing the string "¿" out using
System.out.println(new String("¿".getBytes("UTF-8")));
¿ is written instead of just ¿.
WHY? And how do we fix it?
回答1:
You don't have to use UTF-16 to solve this:
new String("¿".getBytes("UTF-8"), "UTF-8");
works just fine. As long as the encoding given to the getBytes() method is the same as the encoding you pass to the String constructor, you should be fine!
回答2:
You need to specify the Charset in the String constructor (see the API docs).
回答3:
Try:
System.out.println(new String("¿".getBytes("UTF-8"), "UTF-8"));
You need to specify the encoding both when converting the string to bytes and when converting the bytes back to a string.
回答4:
Sounds like the system console isn't in UTF-8
来源:https://stackoverflow.com/questions/176084/why-does-the-string-get-translated-to-%c3%82-when-calling-getbytes