網路傳輸、序列化、二進制協議解析常需在 byte[] 與基本類型間轉換。本文提供完整、可直接使用的工具類,涵蓋有號/無號、大端/小端場景。
public class ByteUtil {
// short (2 bytes)
public static void putShort(byte[] b, short s, int index) {
b[index + 1] = (byte)(s >> 8);
b[index + 0] = (byte)(s >> 0);
}
public static short getShort(byte[] b, int index) {
return (short)(((b[index + 1] << 8) | (b[index + 0] & 0xFF)));
}
// int (4 bytes)
public static void putInt(byte[] b, int x, int index) {
b[index + 3] = (byte)(x >> 24);
b[index + 2] = (byte)(x >> 16);
b[index + 1] = (byte)(x >> 8);
b[index + 0] = (byte)(x >> 0);
}
public static int getInt(byte[] b, int index) {
return ((b[index + 3] & 0xFF) << 24) |
((b[index + 2] & 0xFF) << 16) |
((b[index + 1] & 0xFF) << 8) |
((b[index + 0] & 0xFF) << 0);
}
// long (8 bytes)
public static void putLong(byte[] b, long x, int index) {
b[index + 7] = (byte)(x >> 56);
b[index + 6] = (byte)(x >> 48);
b[index + 5] = (byte)(x >> 40);
b[index + 4] = (byte)(x >> 32);
b[index + 3] = (byte)(x >> 24);
b[index + 2] = (byte)(x >> 16);
b[index + 1] = (byte)(x >> 8);
b[index + 0] = (byte)(x >> 0);
}
public static long getLong(byte[] b, int index) {
return (((long)b[index + 7] & 0xFF) << 56) |
(((long)b[index + 6] & 0xFF) << 48) |
(((long)b[index + 5] & 0xFF) << 40) |
(((long)b[index + 4] & 0xFF) << 32) |
(((long)b[index + 3] & 0xFF) << 24) |
(((long)b[index + 2] & 0xFF) << 16) |
(((long)b[index + 1] & 0xFF) << 8) |
(((long)b[index + 0] & 0xFF) << 0);
}
// float (4 bytes) - 透過 intBits 轉換
public static void putFloat(byte[] b, float x, int index) {
int bits = Float.floatToIntBits(x);
putInt(b, bits, index);
}
public static float getFloat(byte[] b, int index) {
int bits = getInt(b, index);
return Float.intBitsToFloat(bits);
}
// double (8 bytes) - 透過 longBits 轉換
public static void putDouble(byte[] b, double x, int index) {
long bits = Double.doubleToLongBits(x);
putLong(b, bits, index);
}
public static double getDouble(byte[] b, int index) {
long bits = getLong(b, index);
return Double.longBitsToDouble(bits);
}
}
public class DataTypeChangeHelper {
// 無號 byte 轉 int (0-255)
public static int unsignedByteToInt(byte b) {
return b & 0xFF;
}
// byte 轉十六進制字串
public static String byteToHex(byte b) {
return Integer.toHexString(b & 0xFF);
}
// 4 bytes 無號轉 long
public static long unsigned4BytesToInt(byte[] buf, int pos) {
return ((buf[pos] & 0xFFL) << 24) |
((buf[pos+1] & 0xFFL) << 16) |
((buf[pos+2] & 0xFFL) << 8) |
((buf[pos+3] & 0xFFL));
}
// 基本類型 → byte[] (大端)
public static byte[] shortToBytes(short s) {
return new byte[]{(byte)(s >> 8), (byte)(s >> 0)};
}
public static byte[] intToBytes(int i) {
return new byte[]{(byte)(i >> 24), (byte)(i >> 16), (byte)(i >> 8), (byte)(i >> 0)};
}
public static byte[] longToBytes(long l) {
return new byte[]{
(byte)(l >> 56), (byte)(l >> 48), (byte)(l >> 40), (byte)(l >> 32),
(byte)(l >> 24), (byte)(l >> 16), (byte)(l >> 8), (byte)(l >> 0)
};
}
// byte[] → 基本類型 (大端)
public static short bytesToShort(byte[] b) {
return (short)(((b[0] & 0xFF) << 8) | (b[1] & 0xFF));
}
public static int bytesToInt(byte[] b) {
return ((b[0] & 0xFF) << 24) | ((b[1] & 0xFF) << 16) | ((b[2] & 0xFF) << 8) | (b[3] & 0xFF);
}
public static long bytesToLong(byte[] b) {
return ((long)b[0] & 0xFFL) << 56 | ((long)b[1] & 0xFFL) << 48 |
((long)b[2] & 0xFFL) << 40 | ((long)b[3] & 0xFFL) << 32 |
((long)b[4] & 0xFFL) << 24 | ((long)b[5] & 0xFFL) << 16 |
((long)b[6] & 0xFFL) << 8 | ((long)b[7] & 0xFFL);
}
}
ByteBuffer 預設大端floatToIntBits/doubleToLongBits,不可直接位移ByteBuffer 簡化:ByteBuffer.wrap(bytes).getInt()