<br/>
在網(wǎng)絡(luò)編程中,出于節(jié)約帶寬或者編碼的需要,通常需要以原生方式處理long和int,而不是轉(zhuǎn)換為string。
public class ByteOrderUtils {
public static byte[] int2byte(int res) {?
byte[] targets = new byte[4];?
targets[3] = (byte) (res & 0xff);// 最低位?
targets[2] = (byte) ((res >> 8) & 0xff);// 次低位?
targets[1] = (byte) ((res >> 16) & 0xff);// 次高位?
targets[0] = (byte) (res >>> 24);// 最高位,無符號右移。?
return targets;?
}
public static int byteArrayToInt(byte[] b){?
byte[] a = new byte[4];?
int i = a.length - 1,j = b.length - 1;?
for (; i >= 0 ; i--,j--) {//從b的尾部(即int值的低位)開始copy數(shù)據(jù)?
if(j >= 0)?
a[i] = b[j];?
else?
a[i] = 0;//如果b.length不足4,則將高位補(bǔ)0?
}?
int v0 = (a[0] & 0xff) int v1 = (a[1] & 0xff) int v2 = (a[2] & 0xff) int v3 = (a[3] & 0xff) ;?
return v0 + v1 + v2 + v3;?
}
public static byte[] long2byte(long res) {?
byte[] buffer = new byte[8];?
for (int i = 0; i int offset = 64 - (i + 1) * 8;?
buffer[i] = (byte) ((res >> offset) & 0xff);?
}
return buffer;
}
public static long byteArrayToLong(byte[] b){?
long values = 0;?
for (int i = 0; i values }?
return values;?
}
立即學(xué)習(xí)“Java免費(fèi)學(xué)習(xí)筆記(深入)”;
}
<br/>
java怎么學(xué)習(xí)?java怎么入門?java在哪學(xué)?java怎么學(xué)才快?不用擔(dān)心,這里為大家提供了java速學(xué)教程(入門到精通),有需要的小伙伴保存下載就能學(xué)習(xí)啦!
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://m.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號