Java - 在Eclipse中显示Windows -1252个字符

时间:2017-07-30 19:21:06

标签: java eclipse

我试图通过在Java中使用它们的数字等价物作为用户的输入来显示一些Windows-1252字符(使用Eclipse)。我希望该字符显示在链接中:https://www.w3schools.com/charsets/ref_html_ansi.asp

我尝试使用数值152和149,但它们在Eclipse控制台中显示为问号。我在Eclipse中的代码:

import java.io.IOException;
import java.util.Scanner;
public class Encoding_CP1252 {

    public static void main(String[] args) throws IOException {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a number:");
        int a = in.nextInt();   
        System.out.println("You Entered: " +a);
        char b = (char) a;
        System.out.println("Windows-1252 equivalent is: "+b);
    }
}

我甚至尝试在Run中调整Eclipse配置 - >运行配置 - >常用标签 - >编码设置为'默认-inherited(Cp1252)'但结果仍然相同。

1 个答案:

答案 0 :(得分:1)

  

The char data type [...] are based on the original Unicode specification, which defined characters as fixed-width 16-bit entities.

由于此char b = (char) a;不起作用,您必须改为使用new String(byte[] bytes, String charsetName)

...
byte[] b = {(byte) a};
System.out.println("Windows-1252 equivalent is: "+ new String(b, "Windows-1252"));