/** This class provides methods for converting between byte values * and int's in the range 0..255. In Java, the universe of values * associated with the type byte is -128..127. This class makes it * convenient to use values of type byte to represent values in the * range 0..255. The mapping is simply that byte values normally * interpreted as having the values -128..-1 are instead interpreted * to have values 128..255, respectively. This corresponds to * interpreting the leading bit to be in the "128 column" rather * than, as in 2's complement notation, the "-128 column". * */ public class BytePositiveIntConverter{ /** Returns an int value corresponding to the given byte, which is * assumed to encode an integer in the range 0..255 using a straight * binary encoding. * * @return (int)k if k >= 0; (int)(k+256) otherwise */ public int byteToPosInt(byte k) { return (int)(k >= 0 ? k : k + 256); } /** Returns a byte value that encodes a non-negative int in the * range 0..255 using a straight binary encoding. * * pre: 0 <= m < 256 * @return m if m < 128 and m - 256 otherwise */ public byte posIntToByte(int m) { return (byte)(m < 128 ? m : m - 256); } }