java 将字符串从代码页 1252 转换为 1250

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4850557/
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-10-30 08:18:40  来源:igfitidea点击:

Convert string from codepage 1252 to 1250

javacodepagescp1252cp1250

提问by rafalry

How can I convert one Stringwith characters decoded in codepage 1252 into a Stringdecoded in codepage 1250.

如何将String带有在代码页 1252 中String解码的字符的字符转换为在代码页 1250 中解码的字符。

For example

例如

String str1252 = "ê1?????3ó";
String str1250 = convert(str1252);
System.out.print(str1250);

I want to find such convert()function, that printed output would be:

我想找到这样的convert()功能,打印输出将是:

??????ń?ó

These are Polish-specific characters.

这些是波兰语特有的字符。

Thank you for any suggestions.

谢谢你的任何建议。

回答by axtavt

It's pretty straightforward:

这很简单:

public String convert(String s) {
    return new String(s.getBytes("Windows-1252"), "Windows-1250");
}

Note that System.out.print()can introduce another incorrect conversion due to mismatch between ANSI and OEM code pages. However System.console().writer().print()should output it correctly.

请注意,由于 ANSI 和 OEM 代码页之间的不匹配System.out.print()可能会引入另一个不正确的转换。但是System.console().writer().print()应该正确输出。