|||
如何获得Java无符号byte的值?
/**
* byte被看作无符号值转换成short
* @param x
* @return
*/
public static short getUnsignedByteValue(final byte x) {
ByteBuffer tmpBuffer = ByteBuffer.allocate(2);
tmpBuffer.put(new byte[]{0x00,x});
tmpBuffer.flip();
tmpBuffer.order(ByteOrder.BIG_ENDIAN);
return tmpBuffer.getShort();
}
例如:
byte x = (byte) 0xff;
short unsignedX = getUnsignedByteValue(x);
加我私人微信,交流技术。
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-5-19 16:28
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社