Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 網絡傳輸中的字節序問題:大小端轉換完全解析

編輯:關於 Android 程式設計

大小端之爭從哪裡來

字節序的問題幾乎和 CPU 的歷史一樣長,根源在於不同處理器家族對「多字節數據怎麼排進內存」這件事有不同的約定。

大端模式(Big-Endian)把數據的高位字節放在低地址,低位字節放在高地址。它的排列方式很像我們把數據當字符串順序書寫:地址由小到大遞增,字節則從最高有效位往最低有效位依次擺放。

小端模式(Little-Endian)恰好相反,高位字節落在高地址,低位字節落在低地址。這種方式讓地址的高低與數值位的權重天然對齊——高地址對應高權值,低地址對應低權值,判斷時更直觀。

有個流傳很廣的比喻:這就像吃甘蔗,有人習慣從根部啃起,有人喜歡從梢部下口。單獨吃一節(char)沒什麼分歧,吃兩節(short)、四節(int)也各自成立。問題出在當不同 CPU 架構的設備要交換數據時——你遞給對方的「甘蔗」,順序並不合他的口味,必須先加工一遍。這就是大小端轉換的由來。

為什麼網絡通信必須做轉換

網絡上傳輸的本質是字節流,也就是一個個 byte 組成的數組。要把 2 字節的 short 或 4 字節的 int 放進這個字節流,就必須決定「誰先誰後」。只有雙方約定同一種排列方式,接收端才能正確還原出原始數值。

好消息是轉換邏輯非常簡單:同一個數值按大端拆出的字節數組,與按小端拆出的結果正好是逆序關係。因此只要把數組反序排列,就能在兩種字節序之間互轉。

轉換函數實現

下面約定 int 佔 4 字節、short 佔 2 字節、byte 佔 1 字節,並分別給出小端與大端的編解碼方法。

/** 小端:低位字節在前 */
public static byte[] little_intToByte(int i, int len) {
    byte[] abyte = new byte[len];
    if (len == 1) {
        abyte[0] = (byte) (0xff & i);
    } else if (len == 2) {
        abyte[0] = (byte) (0xff & i);
        abyte[1] = (byte) ((0xff00 & i) >> 8);
    } else {
        abyte[0] = (byte) (0xff & i);
        abyte[1] = (byte) ((0xff00 & i) >> 8);
        abyte[2] = (byte) ((0xff0000 & i) >> 16);
        abyte[3] = (byte) ((0xff000000 & i) >> 24);
    }
    return abyte;
}

public static int little_bytesToInt(byte[] bytes) {
    int addr = 0;
    if (bytes.length == 1) {
        addr = bytes[0] & 0xFF;
    } else if (bytes.length == 2) {
        addr = bytes[0] & 0xFF;
        addr |= (((int) bytes[1] << 8) & 0xFF00);
    } else {
        addr = bytes[0] & 0xFF;
        addr |= (((int) bytes[1] << 8) & 0xFF00);
        addr |= (((int) bytes[2] << 16) & 0xFF0000);
        addr |= (((int) bytes[3] << 24) & 0xFF000000);
    }
    return addr;
}

/** 大端:高位字節在前,支持 1 或 4 字節 */
public static byte[] big_intToByte(int i, int len) {
    byte[] abyte = new byte[len];
    if (len == 1) {
        abyte[0] = (byte) (0xff & i);
    } else if (len == 2) {
        abyte[0] = (byte) ((i >>> 8) & 0xff);
        abyte[1] = (byte) (i & 0xff);
    } else {
        abyte[0] = (byte) ((i >>> 24) & 0xff);
        abyte[1] = (byte) ((i >>> 16) & 0xff);
        abyte[2] = (byte) ((i >>> 8) & 0xff);
        abyte[3] = (byte) (i & 0xff);
    }
    return abyte;
}

public static int big_bytesToInt(byte[] bytes) {
    int addr = 0;
    if (bytes.length == 1) {
        addr = bytes[0] & 0xFF;
    } else if (bytes.length == 2) {
        addr = bytes[0] & 0xFF;
        addr = (addr << 8) | (bytes[1] & 0xff);
    } else {
        addr = bytes[0] & 0xFF;
        addr = (addr << 8) | (bytes[1] & 0xff);
        addr = (addr << 8) | (bytes[2] & 0xff);
        addr = (addr << 8) | (bytes[3] & 0xff);
    }
    return addr;
}

如何判斷當前設備的字節序

不需要查芯片手冊,用一個 short 做一次位移測試就能得出結論:把 0x1 存入 short,再右移 8 位,如果仍等於 0x1,說明最高位字節在低地址,即大端;否則為小端。

// 判斷本機 CPU 的字節序
public static boolean isBigendian() {
    short i = 0x1;
    boolean bRet = ((i >> 8) == 0x1);
    Log.i(tags, "bRet = " + bRet);
    return bRet;
}

掌握這套編碼/解碼方法後,跨平台、跨設備的二進制協議對接就不再會因為字節序而出錯。工程實踐中,網絡協議通常統一到網絡字節序(大端),Java 的 ByteBuffer、DataOutputStream 等也默認採用大端,理解底層原理有助於排查那些「數值莫名變大幾百倍」的離奇問題。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved