Search code examples
javaprocessingprintwriter

Processing PrintWriter not working correctly


I have been really confused on this, and I've been trying to solve it for about the past 4 hours. But now I give up. I am trying to write bytes to a file. Let's call it test.bin. If I use my Printwriter and do

pw.write(0xff);

or

pw.write((char)0xff);

or

pw.write((byte)0xff);

And I look at the file in a hex editor, None of them actually write 0xFF. They write 0xC3BF. If I do pw.write(0x00); then it actually writes 0x00. I assume this is because it is signed? If so, how do I get around this? All I want to do is write 0xFF to a file :(


Solution

  • You are writing in UTF8 encoding. 0xFF in UTF8 is 0xC3BF. Use Outputstream instead of Printwriter and itll work