CharBuffer和ByteBuffer - charset编码

时间:2017-10-31 05:10:58

标签: java buffer java-io bytebuffer charbuffer

Java以UCS-2格式存储字符。

    byte[] bytes = {0x00, 0x48, 0x00, 0x69, 0x00, 0x2c,
                    0x60, (byte)0xA8, 0x59, 0x7D, 0x00, 0x21};
    // Print UCS-2 in hex codes
    System.out.printf("%10s", "UCS-2");
    for(int i=0; i<bytes.length; i++) {
        System.out.printf("%02x", bytes[i]);
    }

1) 在下面的代码中,

    Charset charset = Charset.forName("UTF-8");
    // Encode from UCS-2 to UTF-8
    // Create a ByteBuffer by wrapping a byte array
    ByteBuffer bb = ByteBuffer.wrap(bytes);

用于bytes bb wrap()的{​​{1}}存储的字节顺序是多少? BigEndian还是LittleEndian?

2) 在下面的代码中,

    // Create a CharBuffer from a view of this ByteBuffer
    CharBuffer cb = bb.asCharBuffer();
    ByteBuffer bbOut = charset.encode(cb);

用于将bb字节存储为cbasCharBuffer()的字符的编码格式是什么?

0 个答案:

没有答案