gpt4 book ai didi

java - getBytes ("UTF-8")、getBytes ("windows-1252") 和 getBytes() 之间有什么区别?

转载 作者:行者123 更新时间:2023-12-01 21:36:28 30 4
gpt4 key购买 nike

我有以下代码,它会产生令人困惑的输出..

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

public class Main {

String testString = "Moage test String";

public static void main(String[] args) {
new Main();
}

public Main(){

System.out.println("Default charset: "+Charset.defaultCharset());
System.out.println("Teststring: "+testString);
System.out.println();
System.out.println("get the byteStreeam of the test String...");
System.out.println();
System.out.println("Bytestream with default encoding: ");
for(int i = 0; i < testString.getBytes().length; i++){
System.out.print(testString.getBytes()[i]);
}
System.out.println();
System.out.println();
System.out.println("Bytestream with encoding UTF-8: ");
try {
for(int i = 0; i < testString.getBytes("UTF-8").length; i++){
System.out.print(testString.getBytes("UTF-8")[i]);
}
System.out.println();
System.out.println();
System.out.println("Bytestream with encoding windows-1252 (default): ");

for(int i = 0; i < testString.getBytes("windows-1252").length; i++){
System.out.print(testString.getBytes("windows-1252")[i]);
}

System.out.println();
System.out.println();
System.out.println("Bytestream with encoding UTF-16: ");

for(int i = 0; i < testString.getBytes("UTF-16").length; i++){
System.out.print(testString.getBytes("UTF-16")[i]);
}

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}

所以我想看看utf-8编码和windows-1252之间的区别。但当我查看输出时,似乎没有什么区别。只有当我将 windows-1252 与 utf-16 进行 cdompare 时,才会有区别。

输出:

> Default charset: windows-1252 Teststring: Moage test String
>
> get the byteStreeam of the test String...
>
> Bytestream with default encoding:
> 7711197103101321161011151163283116114105110103
>
> Bytestream with encoding UTF-8:
> 7711197103101321161011151163283116114105110103
>
> Bytestream with encoding windows-1252 (default):
> 7711197103101321161011151163283116114105110103
>
> Bytestream with encoding UTF-16:
> -2-1077011109701030101032011601010115011603208301160114010501100103

谁能解释一下为什么utf-8和windows-1252看起来一样

干杯亚历克斯

最佳答案

这是因为您在测试String中仅使用ASCII字符,在您的情况下是“Moage test String”,请尝试使用特殊例如,诸如 "éèà" 之类的字符,您将看到不同的结果。

关于java - getBytes ("UTF-8")、getBytes ("windows-1252") 和 getBytes() 之间有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36909277/

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