0

I have the following code.

String s="AAAA"
ByteBuffer newBuffer = ByteBuffer.allocate(100);
CharBuffer cbuf = newBuffer.asCharBuffer();
for (char c : s.toCharArray()) {
         cbuf.put(c);
}

When I get the cbuf it's in the form 0 , 65 , 0 , 65 , 0 , 65 , 0 , 65.Why there are additional zeros in cbuf (CharBuffer)?

Hussey123
  • 479
  • 1
  • 5
  • 21
  • 4
    This looks like something to do with UTF-16 character encoding - chars are 16-bits in Java, bytes are 8-bit – BretC Dec 08 '16 at 11:55
  • 2
    *The char data type is a single 16-bit Unicode character* and 16 bits is 2 bytes. (E.g `A` in UTF-16 is `0x00 0x41`) – Alex K. Dec 08 '16 at 11:55

0 Answers0