Why does the string “¿” get translated to “¿” when calling .getBytes()

浪尽此生 提交于 2019-12-23 23:27:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!