我有以下代码会产生令人困惑的输出..
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的区别。但是当我查看输出时,似乎没有区别。只有当我使用 utf-16 对 windows-1252 进行 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 看起来一样?
干杯亚历克斯