byte 轉(zhuǎn)換基本數(shù)據(jù)類型
- kotlin 中可以直接調(diào)用 toByte , toInt 之類的函數(shù)直接獲取征峦。
- java 中需要自己獲取邻遏,獲取方式會放到工具類中滴须。
大端和小端
下面是對連個模式的簡單解釋。
- 大端模式:是指數(shù)據(jù)的高字節(jié)保存在內(nèi)存的低地址中朽合,而數(shù)據(jù)的低字節(jié)保存在內(nèi)存的高地址中俱两,這樣的存儲模式有點兒類似于 把數(shù)據(jù)當(dāng)作字符串順序處理:地址由小向大增加,而數(shù)據(jù)從高位往低位放曹步;這和我們的閱讀習(xí)慣一致宪彩。
- 小端模式:是指數(shù)據(jù)的高字節(jié)保存在內(nèi)存的高地址中,而數(shù)據(jù)的低字節(jié)保存在內(nèi)存的低地址中讲婚。
更詳細(xì)的可以參考這篇博客尿孔,詳解大端模式和小端模式。
函數(shù)列表:
int2BytesBig :將int轉(zhuǎn)為高字節(jié)在前,低字節(jié)在后的byte數(shù)組(大端)
int2BytesLittle :將int轉(zhuǎn)為低字節(jié)在前活合,高字節(jié)在后的byte數(shù)組(小端)
bytes2IntLittle :byte數(shù)組到int的轉(zhuǎn)換(小端)
bytes2IntBig :byte數(shù)組到int的轉(zhuǎn)換(大端)
short2BytesBig :將short轉(zhuǎn)為高字節(jié)在前雏婶,低字節(jié)在后的byte數(shù)組(大端)
short2BytesLittle :將short轉(zhuǎn)為低字節(jié)在前,高字節(jié)在后的byte數(shù)組(小端)
bytes2ShortLittle :讀取小端byte數(shù)組為short
bytes2ShortBig :讀取大端byte數(shù)組為short
long2BytesBig :long類型轉(zhuǎn)byte[] (大端)
long2BytesLittle :long類型轉(zhuǎn)byte[] (小端)
bytes2LongLittle :byte[]轉(zhuǎn)long類型(小端)
bytes2LongBig :byte[]轉(zhuǎn)long類型(大端)
long2BytesBOS :long類型轉(zhuǎn)byte[] (大端),通過ByteArrayOutputStream實現(xiàn)
bytes2LongBOS :byte[]轉(zhuǎn)long類型(大端),通過ByteArrayOutputStream實現(xiàn)
long2BytesByteBuffer:long類型轉(zhuǎn)byte[] (大端),通過ByteBuffer實現(xiàn)
bytes2LongByteBuffer:byte[]轉(zhuǎn)long類型(大端), 通過ByteBuffer實現(xiàn)
bytes2Long :將字節(jié)數(shù)組轉(zhuǎn)為long,支持大小端
bytes2LongByteBuffer:將字節(jié)數(shù)組轉(zhuǎn)為long,支持大小端
int2Byte :int 轉(zhuǎn) byte
byte2Int :byte 轉(zhuǎn) int,解決java中byte輸出可能為負(fù)數(shù)的問題
object2Bytes :將 object --> byte 數(shù)組
bytes2Object :數(shù)組轉(zhuǎn)對象
bytesEquals :兩個byte[]是否相同
subBytes :截取byte[]
bytesMerger :拼接byte[] 和 byte[]
bytesMerger :拼接byte 和 byte[]
bytesMerger :拼接byte[] 和 byte
bytesMerger :拼接三個數(shù)組
工具類
public class BytesUtils {
/**
* 將int轉(zhuǎn)為高字節(jié)在前白指,低字節(jié)在后的byte數(shù)組(大端)
*
* @param n int
* @return byte[]
*/
public static byte[] int2BytesBig(int n) {
byte[] b = new byte[4];
b[3] = (byte) (n & 0xff);
b[2] = (byte) (n >> 8 & 0xff);
b[1] = (byte) (n >> 16 & 0xff);
b[0] = (byte) (n >> 24 & 0xff);
return b;
}
/**
* 將int轉(zhuǎn)為低字節(jié)在前留晚,高字節(jié)在后的byte數(shù)組(小端)
*
* @param n int
* @return byte[]
*/
public static byte[] int2BytesLittle(int n) {
byte[] b = new byte[4];
b[0] = (byte) (n & 0xff);
b[1] = (byte) (n >> 8 & 0xff);
b[2] = (byte) (n >> 16 & 0xff);
b[3] = (byte) (n >> 24 & 0xff);
return b;
}
/**
* byte數(shù)組到int的轉(zhuǎn)換(小端)
*
* @param bytes
* @return
*/
public static int bytes2IntLittle(byte[] bytes) {
int int1 = bytes[0] & 0xff;
int int2 = (bytes[1] & 0xff) << 8;
int int3 = (bytes[2] & 0xff) << 16;
int int4 = (bytes[3] & 0xff) << 24;
return int1 | int2 | int3 | int4;
}
/**
* byte數(shù)組到int的轉(zhuǎn)換(大端)
*
* @param bytes
* @return
*/
public static int bytes2IntBig(byte[] bytes) {
int int1 = bytes[3] & 0xff;
int int2 = (bytes[2] & 0xff) << 8;
int int3 = (bytes[1] & 0xff) << 16;
int int4 = (bytes[0] & 0xff) << 24;
return int1 | int2 | int3 | int4;
}
/**
* 將short轉(zhuǎn)為高字節(jié)在前,低字節(jié)在后的byte數(shù)組(大端)
*
* @param n short
* @return byte[]
*/
public static byte[] short2BytesBig(short n) {
byte[] b = new byte[2];
b[1] = (byte) (n & 0xff);
b[0] = (byte) (n >> 8 & 0xff);
return b;
}
/**
* 將short轉(zhuǎn)為低字節(jié)在前告嘲,高字節(jié)在后的byte數(shù)組(小端)
*
* @param n short
* @return byte[]
*/
public static byte[] short2BytesLittle(short n) {
byte[] b = new byte[2];
b[0] = (byte) (n & 0xff);
b[1] = (byte) (n >> 8 & 0xff);
return b;
}
/**
* 讀取小端byte數(shù)組為short
*
* @param b
* @return
*/
public static short bytes2ShortLittle(byte[] b) {
return (short) (((b[1] << 8) | b[0] & 0xff));
}
/**
* 讀取大端byte數(shù)組為short
*
* @param b
* @return
*/
public static short bytes2ShortBig(byte[] b) {
return (short) (((b[0] << 8) | b[1] & 0xff));
}
/**
* long類型轉(zhuǎn)byte[] (大端)
*
* @param n
* @return
*/
public static byte[] long2BytesBig(long n) {
byte[] b = new byte[8];
b[7] = (byte) (n & 0xff);
b[6] = (byte) (n >> 8 & 0xff);
b[5] = (byte) (n >> 16 & 0xff);
b[4] = (byte) (n >> 24 & 0xff);
b[3] = (byte) (n >> 32 & 0xff);
b[2] = (byte) (n >> 40 & 0xff);
b[1] = (byte) (n >> 48 & 0xff);
b[0] = (byte) (n >> 56 & 0xff);
return b;
}
/**
* long類型轉(zhuǎn)byte[] (小端)
*
* @param n
* @return
*/
public static byte[] long2BytesLittle(long n) {
byte[] b = new byte[8];
b[0] = (byte) (n & 0xff);
b[1] = (byte) (n >> 8 & 0xff);
b[2] = (byte) (n >> 16 & 0xff);
b[3] = (byte) (n >> 24 & 0xff);
b[4] = (byte) (n >> 32 & 0xff);
b[5] = (byte) (n >> 40 & 0xff);
b[6] = (byte) (n >> 48 & 0xff);
b[7] = (byte) (n >> 56 & 0xff);
return b;
}
/**
* byte[]轉(zhuǎn)long類型(小端)
*
* @param array
* @return
*/
public static long bytes2LongLittle(byte[] array) {
return ((((long) array[0] & 0xff))
| (((long) array[1] & 0xff) << 8)
| (((long) array[2] & 0xff) << 16)
| (((long) array[3] & 0xff) << 24)
| (((long) array[4] & 0xff) << 32)
| (((long) array[5] & 0xff) << 40)
| (((long) array[6] & 0xff) << 48)
| (((long) array[7] & 0xff) << 56));
}
/**
* byte[]轉(zhuǎn)long類型(大端)
*
* @param array
* @return
*/
public static long bytes2LongBig(byte[] array) {
return ((((long) array[0] & 0xff) << 56)
| (((long) array[1] & 0xff) << 48)
| (((long) array[2] & 0xff) << 40)
| (((long) array[3] & 0xff) << 32)
| (((long) array[4] & 0xff) << 24)
| (((long) array[5] & 0xff) << 16)
| (((long) array[6] & 0xff) << 8)
| (((long) array[7] & 0xff)));
}
/**
* long類型轉(zhuǎn)byte[] (大端),通過ByteArrayOutputStream實現(xiàn)
*
* @param l
* @return
* @throws IOException
*/
public static byte[] long2BytesBOS(long l) throws IOException {
ByteArrayOutputStream bao = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bao);
dos.writeLong(l);
byte[] buf = bao.toByteArray();
return buf;
}
/**
* byte[]轉(zhuǎn)long類型(大端),通過ByteArrayOutputStream實現(xiàn)
*
* @param data
* @return
* @throws IOException
*/
public long bytes2LongBOS(byte[] data) throws IOException {
ByteArrayInputStream bai = new ByteArrayInputStream(data);
DataInputStream dis = new DataInputStream(bai);
return dis.readLong();
}
/**
* long類型轉(zhuǎn)byte[] (大端),通過ByteBuffer實現(xiàn)
*
* @param value
* @return
*/
public static byte[] long2BytesByteBuffer(long value) {
return ByteBuffer.allocate(Long.SIZE / Byte.SIZE).putLong(value).array();
}
/**
* byte[]轉(zhuǎn)long類型(大端), 通過ByteBuffer實現(xiàn)
*
* @param bytes
* @return
*/
public static long bytes2LongByteBuffer(byte[] bytes) {
ByteBuffer buffer = ByteBuffer.allocate(8);
buffer.put(bytes, 0, bytes.length);
buffer.flip();
return buffer.getLong();
}
/**
* 將字節(jié)數(shù)組轉(zhuǎn)為long<br>
* 如果input為null,或offset指定的剩余數(shù)組長度不足8字節(jié)則拋出異常
*
* @param input byte[]
* @param offset 起始偏移量 0
* @param littleEndian 輸入數(shù)組是否小端模式
* @return
*/
public static long bytes2Long(byte[] input, int offset, boolean littleEndian) {
long value = 0;
// 循環(huán)讀取每個字節(jié)通過移位運(yùn)算完成long的8個字節(jié)拼裝
for (int count = 0; count < 8; ++count) {
int shift = (littleEndian ? count : (7 - count)) << 3;
value |= ((long) 0xff << shift) & ((long) input[offset + count] << shift);
}
return value;
}
/**
* 利用 {@link java.nio.ByteBuffer}實現(xiàn)byte[]轉(zhuǎn)long
*
* @param input byte[]
* @param offset 0
* @param littleEndian 輸入數(shù)組是否小端模式
* @return
*/
public static long bytes2LongByteBuffer(byte[] input, int offset, boolean littleEndian) {
// 將byte[] 封裝為 ByteBuffer
ByteBuffer buffer = ByteBuffer.wrap(input, offset, 8);
if (littleEndian) {
// ByteBuffer.order(ByteOrder) 方法指定字節(jié)序,即大小端模式(BIG_ENDIAN/LITTLE_ENDIAN)
// ByteBuffer 默認(rèn)為大端(BIG_ENDIAN)模式
buffer.order(ByteOrder.LITTLE_ENDIAN);
}
return buffer.getLong();
}
/**
* int 轉(zhuǎn) byte
*
* @param t
* @return
*/
public static byte int2Byte(int t) {
return (byte) t;
}
/**
* byte 轉(zhuǎn) int,解決java中byte輸出可能為負(fù)數(shù)的問題
*
* @param b
* @return
*/
public static int byte2Int(byte b) {
return b & 0xFF;
}
/**
* 將 object --> byte 數(shù)組
*
* @param obj
* @return
*/
public static byte[] object2Bytes(Object obj) {
byte[] bytes = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(obj);
oos.flush();
bytes = bos.toByteArray();
oos.close();
bos.close();
} catch (IOException ex) {
ex.printStackTrace();
}
return bytes;
}
/**
* 數(shù)組轉(zhuǎn)對象
*
* @param bytes
* @return
*/
public Object bytes2Object(byte[] bytes) {
Object obj = null;
try {
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bis);
obj = ois.readObject();
ois.close();
bis.close();
} catch (IOException | ClassNotFoundException ex) {
ex.printStackTrace();
}
return obj;
}
/**
* 兩個byte[]是否相同
* @param data1
* @param data2
* @return
*/
public static boolean bytesEquals(byte[] data1, byte[] data2) {
return Arrays.equals(data1, data2);
}
/**
* 截取byte[]
* @param data
* @param position
* @param length
* @return
*/
public static byte[] subBytes(byte[] data, int position, int length) {
byte[] temp = new byte[length];
System.arraycopy(data, position, temp, 0, length);
return temp;
}
/**
* 拼接byte[] 和 byte[]
*
* @param bytes1
* @param bytes2
* @return
*/
public static byte[] bytesMerger(byte[] bytes1, byte[] bytes2) {
byte[] bytes3 = new byte[bytes1.length + bytes2.length];
System.arraycopy(bytes1, 0, bytes3, 0, bytes1.length);
System.arraycopy(bytes2, 0, bytes3, bytes1.length, bytes2.length);
return bytes3;
}
/**
* 拼接byte 和 byte[]
*
* @param byte1
* @param bytes2
* @return
*/
public static byte[] bytesMerger(byte byte1, byte[] bytes2) {
byte[] bytes3 = new byte[1 + bytes2.length];
bytes3[0] = byte1;
System.arraycopy(bytes2, 0, bytes3, 1, bytes2.length);
return bytes3;
}
/**
* 拼接byte[] 和 byte
*
* @param bytes1
* @param byte2
* @return
*/
public static byte[] bytesMerger(byte[] bytes1, byte byte2) {
byte[] bytes3 = new byte[1 + bytes1.length];
System.arraycopy(bytes1, 0, bytes3, 0, bytes1.length);
bytes3[bytes3.length - 1] = byte2;
return bytes3;
}
/**
* 拼接三個數(shù)組
*
* @param bt1
* @param bt2
* @param bt3
* @return
*/
public static byte[] bytesMerger(byte[] bt1, byte[] bt2, byte[] bt3) {
byte[] data = new byte[bt1.length + bt2.length + bt3.length];
System.arraycopy(bt1, 0, data, 0, bt1.length);
System.arraycopy(bt2, 0, data, bt1.length, bt2.length);
System.arraycopy(bt3, 0, data, bt1.length + bt2.length, bt3.length);
return data;
}
}