Java中byte、byte數(shù)組與int、long的轉(zhuǎn)換詳解
一、Java 中 byte 和 int 之間的轉(zhuǎn)換源碼:
//byte 與 int 的相互轉(zhuǎn)換 public static byte intToByte(int x) { return (byte) x; } public static int byteToInt(byte b) { //Java 總是把 byte 當(dāng)做有符處理;我們可以通過將其和 0xFF 進(jìn)行二進(jìn)制與得到它的無符值 return b & 0xFF; }
測試代碼:
//測試 int 轉(zhuǎn) byte int int0 = 234; byte byte0 = intToByte(int0); System.out.println("byte0=" + byte0);//byte0=-22 //測試 byte 轉(zhuǎn) int int int1 = byteToInt(byte0); System.out.println("int1=" + int1);//int1=234
二、Java 中 byte 數(shù)組和 int 之間的轉(zhuǎn)換源碼:
//byte 數(shù)組與 int 的相互轉(zhuǎn)換 public static int byteArrayToInt(byte[] b) { return b[3] & 0xFF | (b[2] & 0xFF) << 8 | (b[1] & 0xFF) << 16 | (b[0] & 0xFF) << 24; } public static byte[] intToByteArray(int a) { return new byte[] { (byte) ((a >> 24) & 0xFF), (byte) ((a >> 16) & 0xFF), (byte) ((a >> 8) & 0xFF), (byte) (a & 0xFF) }; }
測試代碼:
//測試 int 轉(zhuǎn) byte 數(shù)組 int int2 = 1417; byte[] bytesInt = intToByteArray(int2); System.out.println("bytesInt=" + bytesInt);//bytesInt=[B@de6ced //測試 byte 數(shù)組轉(zhuǎn) int int int3 = byteArrayToInt(bytesInt); System.out.println("int3=" + int3);//int3=1417
三、Java 中 byte 數(shù)組和 long 之間的轉(zhuǎn)換源碼:
private static ByteBuffer buffer = ByteBuffer.allocate(8); //byte 數(shù)組與 long 的相互轉(zhuǎn)換 public static byte[] longToBytes(long x) { buffer.putLong(0, x); return buffer.array(); } public static long bytesToLong(byte[] bytes) { buffer.put(bytes, 0, bytes.length); buffer.flip();//need flip return buffer.getLong(); }
測試代碼:
//測試 long 轉(zhuǎn) byte 數(shù)組 long long1 = 2223; byte[] bytesLong = longToBytes(long1); System.out.println("bytes=" + bytesLong);//bytes=[B@c17164 //測試 byte 數(shù)組 轉(zhuǎn) long long long2 = bytesToLong(bytesLong); System.out.println("long2=" + long2);//long2=2223
四、整體工具類源碼:
import java.nio.ByteBuffer; public class Test { private static ByteBuffer buffer = ByteBuffer.allocate(8); public static void main(String[] args) { //測試 int 轉(zhuǎn) byte int int0 = 234; byte byte0 = intToByte(int0); System.out.println("byte0=" + byte0);//byte0=-22 //測試 byte 轉(zhuǎn) int int int1 = byteToInt(byte0); System.out.println("int1=" + int1);//int1=234 //測試 int 轉(zhuǎn) byte 數(shù)組 int int2 = 1417; byte[] bytesInt = intToByteArray(int2); System.out.println("bytesInt=" + bytesInt);//bytesInt=[B@de6ced //測試 byte 數(shù)組轉(zhuǎn) int int int3 = byteArrayToInt(bytesInt); System.out.println("int3=" + int3);//int3=1417 //測試 long 轉(zhuǎn) byte 數(shù)組 long long1 = 2223; byte[] bytesLong = longToBytes(long1); System.out.println("bytes=" + bytesLong);//bytes=[B@c17164 //測試 byte 數(shù)組 轉(zhuǎn) long long long2 = bytesToLong(bytesLong); System.out.println("long2=" + long2);//long2=2223 } //byte 與 int 的相互轉(zhuǎn)換 public static byte intToByte(int x) { return (byte) x; } public static int byteToInt(byte b) { //Java 總是把 byte 當(dāng)做有符處理;我們可以通過將其和 0xFF 進(jìn)行二進(jìn)制與得到它的無符值 return b & 0xFF; } //byte 數(shù)組與 int 的相互轉(zhuǎn)換 public static int byteArrayToInt(byte[] b) { return b[3] & 0xFF | (b[2] & 0xFF) << 8 | (b[1] & 0xFF) << 16 | (b[0] & 0xFF) << 24; } public static byte[] intToByteArray(int a) { return new byte[] { (byte) ((a >> 24) & 0xFF), (byte) ((a >> 16) & 0xFF), (byte) ((a >> 8) & 0xFF), (byte) (a & 0xFF) }; } //byte 數(shù)組與 long 的相互轉(zhuǎn)換 public static byte[] longToBytes(long x) { buffer.putLong(0, x); return buffer.array(); } public static long bytesToLong(byte[] bytes) { buffer.put(bytes, 0, bytes.length); buffer.flip();//need flip return buffer.getLong(); } }
運(yùn)行測試結(jié)果:
byte0=-22 int1=234 bytesInt=[B@de6ced int3=1417 bytes=[B@c17164 long2=2223
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
相關(guān)文章
劍指Offer之Java算法習(xí)題精講鏈表與字符串及數(shù)組
跟著思路走,之后從簡單題入手,反復(fù)去看,做過之后可能會(huì)忘記,之后再做一次,記不住就反復(fù)做,反復(fù)尋求思路和規(guī)律,慢慢積累就會(huì)發(fā)現(xiàn)質(zhì)的變化2022-03-03一文教你使用Java?Calendar類進(jìn)行日期計(jì)算
在日常開發(fā)中,我們常常需要進(jìn)行日期計(jì)算,比如計(jì)算兩個(gè)日期之間的天數(shù)、月數(shù),在Java中,我們可以使用Java?Calendar類來進(jìn)行日期計(jì)算,下面就跟隨小編一起來學(xué)習(xí)一下吧2023-10-10如何解決線程太多導(dǎo)致java socket連接池出現(xiàn)的問題
這篇文章主要介紹了如何解決線程太多導(dǎo)致socket連接池出現(xiàn)的問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12SpringBoot和VUE源碼直接整合打包成jar的踩坑記錄
這篇文章主要介紹了SpringBoot和VUE源碼直接整合打包成jar的踩坑記錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03解決idea中@Data標(biāo)簽getset不起作用的問題
這篇文章主要介紹了解決idea中@Data標(biāo)簽getset不起作用的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02