Why does byteArray have a length of 22 instead of 20? Why does byteArray have a length of 22 instead of 20? arrays arrays

Why does byteArray have a length of 22 instead of 20?


Alexander's answer explains why it's there, but not how to get rid of it. You simply need to specify the endianness you want in the encoding name:

String source = "0123456789";byte[] byteArray = source.getBytes("UTF-16LE"); // Or UTF-16BE


May be the first two bytes are the Byte Order Mark. It specifies the order of bytes in each 16-bit word used in the encoding.


Try printing out the bytes in hex to see where the extra 2 bytes are added - are they at the start or end?

I'm picking that you'll find a byte order marker at the start (0xFEFF) - this allows anyone consuming (receiving) the byte array to recognise whether the encoding is little-endian or big-endian.