I think the real question here is, where did this array of bytes come from? It looks like the first three bytes are simply wrong, and the fourth suffers from conversion problems. The correct value of 240 in hex is given by Integer.toHexString(240), which gives "F0". The problem is that a byte in

Java is assumed to have a range from -128 to +127, so when you put 240 = F0 into a single byte, and then try to do other operations with that number, Java assumes that the F0 represents a negative value (-16 in this case). And when you use Integer.toHexString(), which expects an integer argument, Java converts the byte to int

*using sign extension*. This means that since F0 = 11110000 represents a negative value (as far as Java is concerned), a bunch of 1's will be added to the left hand side of the number until you get 11111111 11111111 11111111 11110000 = FFF0 (representing -16 as an int), which is what was printed.

If you really need to know the hex string for a single byte

*without* sign extension (i.e. assuming it represents a value between 0 and 255 rather than -128 to 127), use the following:

<code><pre>

byte b = (byte) 240;

System.out.println(Integer.toHexString(0x000F & b));

</pre></code>

By performing a bitwise AND with 0x000F, we effectively set the first 12 bits of the result to zero, meaning that any sign extension is cancelled out.