java实现对无符号整数的支持
生活随笔
收集整理的這篇文章主要介紹了
java实现对无符号整数的支持
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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实现对无符号整数的支持的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C与java通讯小结
- 下一篇: 深入解析JNA—模拟C语言结构体