0

紛らわしい出力を生成する次のコードがあります..

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 が同じように見える理由を誰か説明できますか?

乾杯アレックス

4

2 に答える 2

3

ASCIIこれは、テストで文字Stringのみを使用するためです。たとえば、"Moage test String"特殊文字を使用"éèà"すると、異なる結果が表示されます。

于 2016-04-28T08:32:53.120 に答える
0

ここ、

の範囲に属する文字列文字を使用しましたASCII。文字列に特殊文字または特殊文字をサポートする言語が含まれている場合、バイト出力は変更されます。

UTF-8 は一般的に承認されている標準であり、どこでも機能します。ただし、Windows-any エンコーディングは Windows 固有のものであり、どのマシンでも動作することが保証されているわけではありません。

于 2016-04-28T08:44:14.913 に答える