Given this code in Java:
FileOutputStream os = new FileOutputStream("/tmp/test.dat");
os.write(0x14);
os.write(0xfe);
os.write(0xae);
os.write(String.valueOf((char) 0x14).getBytes("UTF-8"));
os.write(String.valueOf((char) 0xfe).getBytes("UTF-8"));
os.write(String.valueOf((char) 0xae).getBytes("UTF-8"));
os.write("\u0014".getBytes("UTF-8"));
os.write("\u00fe".getBytes("UTF-8"));
os.write("\u00ae".getBytes("UTF-8"));
os.close();
Can somebody explain to me why the first 3 bytes in test.dat are
14 fe ae
while the output from the last 6 os.write()’s are
14 c3 be c2
Basically, I want to literally output the bytes 14 fe ae. I was storing these values as a String constant, and writing the value of these constants to a UTF-8 file, but 14 c3 be c2 was output instead. There’s obviously a gap in my understanding in how these byte sequences are converted in Java.
Thanks!
It gives:
The first three bytes are obvious. They’re just being outputted literally. For the next three, you should remember that char in Java represents a UTF-16 code unit, not a byte. So you’re first creating the Unicode code units U+0014, U+00FE, U+00AE, then converting each to UTF-8. U+0014 is 0x14 in UTF-8 (since it’s also ASCII), but U+00FE is 0xC3 0xBE and U+00AE is 0xC2 0xAE.
You’re creating the same characters again in the next three lines.
The bottom line is that if you want to store literal bytes, just use a byte array.