java整數(shù)與byte數(shù)組的轉(zhuǎn)換實現(xiàn)代碼
更新時間:2017年07月12日 15:27:49 作者:aotian16
這篇文章主要介紹了java整數(shù)與byte數(shù)組的轉(zhuǎn)換實現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下
java整數(shù)與byte數(shù)組的轉(zhuǎn)換實現(xiàn)代碼
這里對java中整數(shù)與byte數(shù)組的轉(zhuǎn)換進行了實現(xiàn),平時的項目中很少用的到,但是特定需求的時候還是需要的,這里就記錄下,親測可用,
實現(xiàn)代碼:
public class NumberUtil {
/**
* int整數(shù)轉(zhuǎn)換為4字節(jié)的byte數(shù)組
*
* @param i
* 整數(shù)
* @return byte數(shù)組
*/
public static byte[] intToByte4(int i) {
byte[] targets = new byte[4];
targets[3] = (byte) (i & 0xFF);
targets[2] = (byte) (i >> 8 & 0xFF);
targets[1] = (byte) (i >> 16 & 0xFF);
targets[0] = (byte) (i >> 24 & 0xFF);
return targets;
}
/**
* long整數(shù)轉(zhuǎn)換為8字節(jié)的byte數(shù)組
*
* @param lo
* long整數(shù)
* @return byte數(shù)組
*/
public static byte[] longToByte8(long lo) {
byte[] targets = new byte[8];
for (int i = 0; i < 8; i++) {
int offset = (targets.length - 1 - i) * 8;
targets[i] = (byte) ((lo >>> offset) & 0xFF);
}
return targets;
}
/**
* short整數(shù)轉(zhuǎn)換為2字節(jié)的byte數(shù)組
*
* @param s
* short整數(shù)
* @return byte數(shù)組
*/
public static byte[] unsignedShortToByte2(int s) {
byte[] targets = new byte[2];
targets[0] = (byte) (s >> 8 & 0xFF);
targets[1] = (byte) (s & 0xFF);
return targets;
}
/**
* byte數(shù)組轉(zhuǎn)換為無符號short整數(shù)
*
* @param bytes
* byte數(shù)組
* @return short整數(shù)
*/
public static int byte2ToUnsignedShort(byte[] bytes) {
return byte2ToUnsignedShort(bytes, 0);
}
/**
* byte數(shù)組轉(zhuǎn)換為無符號short整數(shù)
*
* @param bytes
* byte數(shù)組
* @param off
* 開始位置
* @return short整數(shù)
*/
public static int byte2ToUnsignedShort(byte[] bytes, int off) {
int high = bytes[off];
int low = bytes[off + 1];
return (high << 8 & 0xFF00) | (low & 0xFF);
}
/**
* byte數(shù)組轉(zhuǎn)換為int整數(shù)
*
* @param bytes
* byte數(shù)組
* @param off
* 開始位置
* @return int整數(shù)
*/
public static int byte4ToInt(byte[] bytes, int off) {
int b0 = bytes[off] & 0xFF;
int b1 = bytes[off + 1] & 0xFF;
int b2 = bytes[off + 2] & 0xFF;
int b3 = bytes[off + 3] & 0xFF;
return (b0 << 24) | (b1 << 16) | (b2 << 8) | b3;
}
}
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
您可能感興趣的文章:
- Java中byte、byte數(shù)組與int、long的轉(zhuǎn)換詳解
- Java 圖片與byte數(shù)組互相轉(zhuǎn)換實例
- Java中字符串與byte數(shù)組之間的相互轉(zhuǎn)換
- Java基本類型與byte數(shù)組之間相互轉(zhuǎn)換方法
- 基于java中byte數(shù)組與int類型的轉(zhuǎn)換(兩種方法)
- 淺談java的byte數(shù)組的不同寫法
- 全面了解java byte數(shù)組與文件讀寫
- 讀取Java文件到byte數(shù)組的三種方法(總結(jié))
- 詳解Java中ByteArray字節(jié)數(shù)組的輸入輸出流的用法
- java對象轉(zhuǎn)成byte數(shù)組的3種方法
相關(guān)文章
Java實現(xiàn)XML與JSON秒級轉(zhuǎn)換示例詳解
這篇文章主要為大家介紹了Java實現(xiàn)XML與JSON秒級轉(zhuǎn)換示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09
Idea如何使用Fast Request接口調(diào)試
這篇文章主要介紹了Idea如何使用Fast Request接口調(diào)試問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
詳解Spring Cache使用Redisson分布式鎖解決緩存擊穿問題
本文主要介紹了詳解Spring Cache使用Redisson分布式鎖解決緩存擊穿問題,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
Java switch case數(shù)據(jù)類型原理解析
這篇文章主要介紹了Java switch case數(shù)據(jù)類型原理解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-01-01

