java实现对无符号整数的支持
生活随笔
收集整理的這篇文章主要介紹了
java实现对无符号整数的支持
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
package cn.miw.hp.test;public class ToUnsigned {
public static long getUnSignedLong(long l) {
return getLong(longToDword(l), 0);
}// 將long型數(shù)據(jù)轉(zhuǎn)換為Dword的字節(jié)數(shù)組(C/C++的無(wú)符號(hào)整數(shù))
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++的無(wú)符號(hào) DWORD類型轉(zhuǎn)換為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;
}
}
總結(jié)
以上是生活随笔為你收集整理的java实现对无符号整数的支持的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: C与java通讯小结
- 下一篇: 深入解析JNA—模拟C语言结构体