It's about the default charset. It may have something to do with the encoding of your java file.
(On my machine, when I compile java file with encoding of cp1252, getBytes()
seems to also use cp1252 as default charset. Since cp1252 doesn't support the unicode character, it becomes a ?
character, i.e. 63
. When I compile java with encoding of UTF-16, getBytes()
returns the data 0x9999
as expected.)
The behavior of this method when this string cannot be encoded in the
default charset is unspecified.
(Source: getBytes()
from oracle.com)
My suggestion is to simply use "\u9999".getBytes(StandardCharsets.UTF_16LE)
(or UTF_16BE
) to get the 2-byte array you desire. So there is no need to be concerned about encoding of java source. The array should be {-103,-103}
.
byte
with value of -103
is represented in memory as 0x99
.