Java Windows-1252 编码 - 显示的字符不正确

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18424354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:31:22  来源:igfitidea点击:

Windows-1252 encoding - incorrect characters displayed

javaencoding

提问by user2707175

I have a buffer with chars encoded in Windows-1252. However when I create a new String with appropriate encoding, instead of expected result I've get quite often interrogation marks, ex.

我有一个用 Windows-1252 编码的字符的缓冲区。然而,当我用适当的编码创建一个新的字符串时,我经常得到询问标记,而不是预期的结果,例如。

byte[] tmps = new byte[] {(byte) 0xfb};
System.out.println (new String (tmps,0,1,"Windows-1252" ));

As result the system should display "u" char with "^" above it. Instead it displays "?".

结果系统应该显示“u”字符,上面有“^”。相反,它显示“?”。

Any idea?

任何的想法?

回答by Stephen C

First of all Windows-1252 isa supported encoding:

首先,Windows-1252一种受支持的编码:

I think that the most likely problem here is on the output side. Specifically, Java may think that your locale's default charset is ASCII or something that doesn't support that codepoint.

我认为这里最有可能的问题出在输出端。具体来说,Java 可能认为您的语言环境的默认字符集是 ASCII 或不支持该代码点的东西。

One way to eliminate Windows-1252as the causeof the problem is to write the equivalent string using a Unicode escape; e.g.

消除的一种方法Windows-1252原因的问题是写使用Unicode转义等价的字符串; 例如

    System.out.println("\u00fb");

回答by user2707175

I've already found this.

我已经找到了这个。

Menu Run/Run configurations/ next Java Application and your own app name/tab common/ next encoding set to UTF-8

菜单运行/运行配置/下一个 Java 应用程序和您自己的应用程序名称/选项卡通用/下一个编码设置为 UTF-8

And since now both windows 1250 and 1252 chars seems to be displayed ok.

从现在开始,Windows 1250 和 1252 个字符似乎都可以正常显示。