java的UTF-16字符编码

pri*_*jan 13 java character-encoding

我试图理解Java中的字符编码.Java中的字符使用UTF-16编码以16位存储.因此,当我将包含6个字符的字符串转换为字节时,我得到6个字节,如下所示,我期待它为12.是否有任何概念我缺少?

package learn.java;

public class CharacterTest {

    public static void main(String[] args) {
        String str = "Hadoop";
        byte bt[] = str.getBytes();
        System.out.println("the length of character array is " + bt.length);
    } 
}
Run Code Online (Sandbox Code Playgroud)

O/p:字符数组的长度为6

根据@Darshan当尝试使用UTF-16编码来获取字节时,结果也没有预料到.

package learn.java;

    public class CharacterTest {

        public static void main(String[] args) {

            String str = "Hadoop";
            try{
                byte bt[] = str.getBytes("UTF-16");
                System.out.println("the length of character array is " + bt.length);

            }
            catch(Exception e)
            {

            }
        } 
    }

o/p: the length of character array is 14
Run Code Online (Sandbox Code Playgroud)

tuc*_*uxi 11

在UTF-16版本中,由于插入了标记以区分Big Endian(默认)和Little Endian,因此您将获得14个字节.如果指定UTF-16LE,则会得到12个字节(little-endian,不添加字节顺序标记).

http://www.unicode.org/faq/utf_bom.html#gen7


编辑 -使用此程序查看由不同编码生成的实际字节:

public class Test {
    public static void main(String args[]) throws Exception {
        // bytes in the first argument, encoded using second argument
        byte[] bs = args[0].getBytes(args[1]);
        System.err.println(bs.length + " bytes:");

        // print hex values of bytes and (if printable), the char itself
        char[] hex = "0123456789ABCDEF".toCharArray();
        for (int i=0; i<bs.length; i++) {
            int b = (bs[i] < 0) ? bs[i] + 256 : bs[i];
            System.err.print(hex[b>>4] + "" + hex[b&0xf] 
                + ( ! Character.isISOControl((char)b) ? ""+(char)b : ".")
                + ( (i%4 == 3) ? "\n" : " "));
        }
        System.err.println();   
    }
}
Run Code Online (Sandbox Code Playgroud)

例如,在UTF-8下运行时(在其他JVM默认编码下,FE和FF的字符将显示不同),输出为:

$ javac Test.java  && java -cp . Test hello UTF-16
12 bytes:
FEþ FFÿ 00. 68h
00. 65e 00. 6Cl
00. 6Cl 00. 6Fo
Run Code Online (Sandbox Code Playgroud)

$ javac Test.java  && java -cp . Test hello UTF-16LE
10 bytes:
68h 00. 65e 00.
6Cl 00. 6Cl 00.
6Fo 00. 
Run Code Online (Sandbox Code Playgroud)

$ javac Test.java  && java -cp . Test hello UTF-16BE
10 bytes:
00. 68h 00. 65e
00. 6Cl 00. 6Cl
00. 6Fo
Run Code Online (Sandbox Code Playgroud)

  • 我很困惑,BOM不应该是FE FF吗?你的打印输出为什么是76~77? (2认同)