开发者

Unable to process special character on command prompt

开发者 https://www.devze.com 2023-03-28 21:45 出处:网络
I am unable to process special characters such as (–, \', £ etc) in a java file when i try executing them on windows command prompt.

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");
   }开发者_JAVA技巧
}

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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号