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

Leave a Comment