byte 數(shù)組工具類

byte 轉(zhuǎn)換基本數(shù)據(jù)類型

  1. kotlin 中可以直接調(diào)用 toByte , toInt 之類的函數(shù)直接獲取征峦。
  2. 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;
    }


}

參考

Java 中 byte错维、byte 數(shù)組和 int、long 之間的轉(zhuǎn)換

大端小端模式

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末橄唬,一起剝皮案震驚了整個濱河市赋焕,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌仰楚,老刑警劉巖宏邮,帶你破解...
    沈念sama閱讀 221,820評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異缸血,居然都是意外死亡蜜氨,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,648評論 3 399
  • 文/潘曉璐 我一進(jìn)店門捎泻,熙熙樓的掌柜王于貴愁眉苦臉地迎上來飒炎,“玉大人,你說我怎么就攤上這事笆豁±赏簦” “怎么了?”我有些...
    開封第一講書人閱讀 168,324評論 0 360
  • 文/不壞的土叔 我叫張陵闯狱,是天一觀的道長煞赢。 經(jīng)常有香客問我,道長哄孤,這世上最難降的妖魔是什么照筑? 我笑而不...
    開封第一講書人閱讀 59,714評論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮瘦陈,結(jié)果婚禮上凝危,老公的妹妹穿的比我還像新娘。我一直安慰自己晨逝,他們只是感情好蛾默,可當(dāng)我...
    茶點故事閱讀 68,724評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著捉貌,像睡著了一般支鸡。 火紅的嫁衣襯著肌膚如雪冬念。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,328評論 1 310
  • 那天牧挣,我揣著相機(jī)與錄音刘急,去河邊找鬼。 笑死浸踩,一個胖子當(dāng)著我的面吹牛叔汁,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播检碗,決...
    沈念sama閱讀 40,897評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼据块,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了折剃?” 一聲冷哼從身側(cè)響起另假,我...
    開封第一講書人閱讀 39,804評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎怕犁,沒想到半個月后边篮,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,345評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡奏甫,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,431評論 3 340
  • 正文 我和宋清朗相戀三年戈轿,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片阵子。...
    茶點故事閱讀 40,561評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡思杯,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出挠进,到底是詐尸還是另有隱情色乾,我是刑警寧澤,帶...
    沈念sama閱讀 36,238評論 5 350
  • 正文 年R本政府宣布领突,位于F島的核電站暖璧,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏君旦。R本人自食惡果不足惜澎办,卻給世界環(huán)境...
    茶點故事閱讀 41,928評論 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望于宙。 院中可真熱鬧浮驳,春花似錦悍汛、人聲如沸捞魁。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,417評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽谱俭。三九已至奉件,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間昆著,已是汗流浹背县貌。 一陣腳步聲響...
    開封第一講書人閱讀 33,528評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留凑懂,地道東北人煤痕。 一個月前我還...
    沈念sama閱讀 48,983評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像接谨,于是被迫代替她去往敵國和親摆碉。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,573評論 2 359

推薦閱讀更多精彩內(nèi)容