package cn.miw.hp.test;
public class ToUnsigned {
public static long getUnSignedLong(long l) {
return getLong(longToDword(l), 0);
}
// 将long型数据转换为Dword的字节数组(C/C++的无符号整数)
private static byte[] longToDword(long value) {
byte[] data = new byte[4];
for (int i = 0; i < data.length; i++) {
data[i] = (byte) (value >> (8 * i));
}
return data;
}
// 将C/C++的无符号 DWORD类型转换为java的long型
private static long getLong(byte buf[], int index) {
int firstByte = (0x000000FF & ((int) buf[index]));
int secondByte = (0x000000FF & ((int) buf[index + 1]));
int thirdByte = (0x000000FF & ((int) buf[index + 2]));
int fourthByte = (0x000000FF & ((int) buf[index + 3]));
long unsignedLong = ((long) (firstByte | secondByte << 8 | thirdByte << 16 | fourthByte << 24)) & 0xFFFFFFFFL;
return unsignedLong;
}
}
java实现对无符号整数的支持
最新推荐文章于 2024-07-17 23:23:30 发布