Есть ли что-нибудь подобное в JDK или Apache Commons (или в другом банке)?
/** * Return the integer positive value of the byte. (e.g. -128 will return * 128; -127 will return 129; -126 will return 130...) */ public static int toPositiveInt(byte b) { int intV = b; if (intV < 0) { intV = -intV; int diff = ((Byte.MAX_VALUE + 1) - intV) + 1; intV = Byte.MAX_VALUE + diff; } return intV; }
Обычно для этого вы используете базовые битовые манипуляции:
public static int toPositiveInt(byte b) { return b & 0xFF; }
И поскольку он очень короткий, он обычно встроен и не вызывается как метод.