gpt4 book ai didi

Java Charset.forName ("ASCII") 或 Charset.forName ("US-ASCII")

转载 作者:搜寻专家 更新时间:2023-10-30 21:19:22 32 4
gpt4 key购买 nike

我正在检查代码并遇到以下行。

Charset.forName("ASCII")

但是当我查看 java documentation它只有

US-ASCII    ISO-8859-1    UTF-8   UTF-16BE   UTF-16LE   UTF-16  

但代码有效。在这种情况下,“ASCII”和“US-ASCII”是同义词吗?还是由于无法识别“ASCII”,代码是否采用了一些默认值?在这种情况下,'ASCII' 代表一个字符多少字节?

最佳答案

文档指出:

Every charset has a canonical name and may also have one or more aliases. The canonical name is returned by the name method of this class. Canonical names are, by convention, usually in upper case. The aliases of a charset are returned by the aliases method.

此外,Charset.forName(String charsetName) 的 javadoc 告诉您:

charsetName - The name of the requested charset; may be either a canonical name or an alias

通过这段代码,您可以找到有关字符集的更多信息:

Charset ascii = Charset.forName("US-ASCII");
System.out.println(ascii.aliases());
// [ANSI_X3.4-1968, cp367, csASCII, iso-ir-6, ASCII, iso_646.irv:1983, ANSI_X3.4-1986, ascii7, default, ISO_646.irv:1991, ISO646-US, IBM367, 646, us]

System.out.println(ascii.newEncoder().maxBytesPerChar());
// 1.0

Charset utf8 = Charset.forName("UTF-8");
System.out.println(utf8.newEncoder().maxBytesPerChar());
// 3.0

关于Java Charset.forName ("ASCII") 或 Charset.forName ("US-ASCII"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32063929/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com