0

JDK或Apache Commons(或其他jar)中是否有类似的东西?

/**
 * 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;
    }
4

1 回答 1

3

通常,您为此使用一些基本的位操作:

public static int toPositiveInt(byte b) {
return b & 0xFF;
}

而且因为它很短,所以通常是内联的,而不是作为方法调用的。

于 2010-04-14T20:54:58.400 回答