I am working on a project where I want to write data into a .png file. The first byte that every .png file contains has a decimal value of 137. But here is the problem: Java only supports signed bytes, so casting 137 to byte ends me up with -119! Is there any way I can solve this?
I mean, converting a signed byte into an unsigned INT is easy, using bit-wise AND. But how do I get that unsigned value of 137 into an actual byte object? Or does it not matter? Will the file still be recognized as .png as long as the first byte is 10001001, no matter how that number is interpreted?
I have tried simply casting 137 to a byte:
byte[] pngIdentifier = {(byte) 137, 80, 78, 71, 13, 10, 26, 10};
and I have tried using the bit-wise AND:
byte[] pngIdentifier = {(byte) -119 &0xff, 80, 78, 71, 13, 10, 26, 10};
but both of these approaches result in -119 as soon as the int is casted back to byte.
Thanks a lot in advance!
It does not matter. 137 and -119 have the same low eight bits.