How can I treat signed bytes as their unsigned values?

John Zukowski

When using methods like InetAddress.getAddress(), a byte array is returned where each byte represents a value from 0 to 255. Just converting from a byte to an int preserves the sign and does not convert any negative values to be between 128 and 255. To convert a specific byte, just add 256, but only when the value is negative.

int unsignedByte = (theByte >= 0) ? theByte : (theByte + 256);