Java字节类型的编码是什么?
What is the encoding of Java byte type?
当我执行以下代码时:
public class Main {
public static void main(String[] args) {
String str = "Hello John";
System.out.println(str.getBytes());
}
}
str.getBytes() 的编码类型是什么。是 ASCII UTF 还是什么?
字节没有编码。它们是字节。
查看 String.getBytes()
的 Javadoc:
Encodes this String into a sequence of bytes using the platform's default charset
因此,无论您的默认字符集是什么,都可以。您可以使用 Charset.defaultCharset()
.
找出运行时的内容
如果您想要特定字符集中的字节,请指定它,例如:
str.getBytes(StandardCharsets.UTF_8)
// or
str.getBytes(Charset.forName("the name of the desired charset"))
(请注意 System.out.println(str.getBytes());
不会打印与数组内容相关的任何内容)。
当我执行以下代码时:
public class Main {
public static void main(String[] args) {
String str = "Hello John";
System.out.println(str.getBytes());
}
}
str.getBytes() 的编码类型是什么。是 ASCII UTF 还是什么?
字节没有编码。它们是字节。
查看 String.getBytes()
的 Javadoc:
Encodes this String into a sequence of bytes using the platform's default charset
因此,无论您的默认字符集是什么,都可以。您可以使用 Charset.defaultCharset()
.
如果您想要特定字符集中的字节,请指定它,例如:
str.getBytes(StandardCharsets.UTF_8)
// or
str.getBytes(Charset.forName("the name of the desired charset"))
(请注意 System.out.println(str.getBytes());
不会打印与数组内容相关的任何内容)。