开发者

byte toPositiveInt method

is there anything like this in JDK or Apache Commons (or in other 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) {
 开发者_C百科    intV = -intV;
     int diff = ((Byte.MAX_VALUE + 1) - intV) + 1;
     intV = Byte.MAX_VALUE + diff;
 }
 return intV;
    }


Usually, you use some basic bit manipulation for this:

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

And because it is so short, it is usually inlined and not called as a method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜