在 Java 中,哪种编码方案是 8 位 US ASCII?

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

in Java, which encoding scheme is 8-bit US ASCII?

javaencodingascii

提问by Martin08

I want to write a string to a file which expects an 8-bit US ASCIIencoding.

我想将一个字符串写入一个需要8-bit US ASCII编码的文件。

Which encoding scheme should I use for the method String.getBytes(encodingScheme)?

我应该为该方法使用哪种编码方案String.getBytes(encodingScheme)

Thanks.

谢谢。

回答by phihag

ASCII is a 7bit encoding scheme, there is no "8-bit ASCII".

ASCII 是 7 位编码方案,没有“8 位 ASCII”。

However, many encodings are ASCII-compatible, and some are 8bit transparent (i.e. every binary series maps to a valid character string, and vice versa, useful if you're sending binary data over a character channel without encoding it in base64 or so). If you just want to be ASCII-compatible, UTF-8is the best choice; if you need 8 bit transparency, ISO-8859-1.

然而,许多编码是 ASCII 兼容的,有些是 8 位透明的(即每个二进制系列映射到一个有效的字符串,反之亦然,如果您通过字符通道发送二进制数据而不使用 base64 左右编码,则非常有用) . 如果只想兼容ASCII,UTF-8是最好的选择;如果您需要 8 位透明度,ISO-8859-1.

Note that the above advice is only useful if you want to transport ASCII-only strings or 8bit binary ones. In most cases, you actually want to transfer arbitrary strings, and there's no way around finding the proper encoding for these.

请注意,上述建议仅在您想传输纯 ASCII 字符串或 8 位二进制字符串时才有用。在大多数情况下,您实际上想要传输任意字符串,并且没有办法为这些找到正确的编码。

回答by Jon Skeet

There's no such thing as "8-bit ASCII". There are several 8-bit "extensions" to ASCII, including ISO-8859-1and Windows-1252. Those are probably the most common ones, but they're not the same. You really need to find out exactlywhich encoding is expected.

没有“8 位 ASCII”这样的东西。ASCII 有几个 8 位“扩展”,包括ISO-8859-1Windows-1252。这些可能是最常见的,但它们并不相同。您确实需要确切地找出预期的编码。

Both of those names are available viathose names in Java - at least they are on my JDK installation. (You may find that Windows-1252 isn't available on a Linux installation, for example.)

这两个名称都可以通过Java 中的这些名称获得 - 至少它们在我的 JDK 安装中。(例如,您可能会发现 Windows-1252 在 Linux 安装中不可用。)