Unable to process special character on command prompt

六眼飞鱼酱① 提交于 2020-01-15 05:43:10

问题


I am unable to process special characters such as (–, ', £ etc) in a java file when i try executing them on windows command prompt.

Example:

public class HelloWorld {
   public static void main(String[] args) {
     System.out.println("Mangifera indica – the common mango");
   }
}

Expected Output: Mangifera indica – the common mango

Ouput Recieved: Mangifera indica ΓÇô the common mango

I believe it is some sort of unicode problem, and i very much want to run it on the command prompt, is there a way it could be sorted?


回答1:


With this little program you can "brute-force" check the encoding of your console. Your input was , resulting in output ΓÇô.

This program will emulate all wrong encoded outputs by trying all available charsets to generate ΓÇô, It just reencode the UTF-8-Bytes of with an other, wrong charset:

String s="–";
byte[] b=s.getBytes("UTF-8");
for (Charset charset:Charset.availableCharsets().values())
{
  String p=new String(b,charset.name());
  if ("ΓÇô".equals(p))
  System.out.println(charset.aliases()+": "+p);
 }

Output:

[ibm-437, windows-437, cspc8codepage437, 437, ibm437, cp437]: ΓÇô

[860, cp860, ibm-860, csIBM860, ibm860]: ΓÇô

[861, cp-is, ibm-861, cp861, csIBM861, ibm861]: ΓÇô

[ibm863, csIBM863, cp863, 863, ibm-863]:ΓÇô

[csIBM865, ibm865, 865, ibm-865, cp865]: ΓÇô

So your console runs in one of these charsets, but your System.out. is assuming UTF-8.




回答2:


I guess the solution to above mentioned problem is to set the windows command prompt font to consola and activate code chcp 65001, worked for me.



来源:https://stackoverflow.com/questions/7112733/unable-to-process-special-character-on-command-prompt

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