本文介绍了字节toPositiveInt方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
JDK或Apache Commons(或其他jar)中是否有类似的东西?
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) {
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.
这篇关于字节toPositiveInt方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!