Actually, if you want to get the same result with the call toString(), specify UTF-16_LEas the encoding of the encoding:
bsh % System.out.println("\u0080".getBytes("UTF-16LE")[0]);
-128
Java strings are encoded internally as UTF-16, and since we need the low byte, as for the char → byte, we use a little end here. Big endian also works if we change the index of the array:
bsh % System.out.println("\u0080".getBytes("UTF-16BE")[1]);
-128
source
share