Why is ¿ displayed different in Windows vs Linux even when using UTF-8?

后端 未结 5 425
小鲜肉
小鲜肉 2021-01-14 20:49

Why is the following displayed different in Linux vs Windows?

System.out.println(new String(\"¿\".getBytes(\"UTF-8\"), \"UTF-8\"));

in Wind

5条回答
  •  失恋的感觉
    2021-01-14 21:18

    Run this code to help determine if it is a compiler or console issue:

    public static void main(String[] args) throws Exception {
        String s = "¿";
        printHex(Charset.defaultCharset(), s);
    
        Charset utf8 = Charset.forName("UTF-8");
        printHex(utf8, s);
    }
    
    public static void printHex(Charset encoding, String s)
            throws UnsupportedEncodingException {
        System.out.print(encoding + "\t" + s + "\t");
    
        byte[] barr = s.getBytes(encoding);
        for (int i = 0; i < barr.length; i++) {
            int n = barr[i] & 0xFF;
            String hex = Integer.toHexString(n);
            if (hex.length() == 1) {
                System.out.print('0');
            }
            System.out.print(hex);
        }
        System.out.println();
    }
    

    If the encoded bytes for UTF-8 are different on each platform (it should be c2bf), it is a compiler issue.

    If it is a compiler issue, replace "¿" with "\u00bf".

提交回复
热议问题