最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java協(xié)議字節(jié)操作工具類(lèi)詳情

 更新時(shí)間:2022年09月07日 11:48:15   作者:何憶清風(fēng)  
這篇文章主要介紹了Java協(xié)議字節(jié)操作工具類(lèi)詳情,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下

前言:

由于最近有解析協(xié)議的一些業(yè)務(wù)場(chǎng)景,需要用到一些字節(jié)操作工具,這里封裝了一些比較常用的轉(zhuǎn)換方法,測(cè)試后基本沒(méi)有問(wèn)題,可能一些比較偏門(mén)的數(shù)據(jù)會(huì)出現(xiàn)數(shù)據(jù)轉(zhuǎn)換錯(cuò)誤

  • int與數(shù)組相互轉(zhuǎn)換
  • short與數(shù)組想換轉(zhuǎn)換
  • 獲取int指定位的數(shù)據(jù)
  • 獲取int指定范圍的位數(shù)據(jù)
  • 對(duì)象轉(zhuǎn)換數(shù)組,數(shù)組轉(zhuǎn)換對(duì)象
public class AppTest {
    /**
     * Rigorous Test :-)
     */
    @Test
    public void shouldAnswerWithTrue() {
        int hex = 16;
        System.out.println("獲取16進(jìn)制字符串?dāng)?shù)據(jù):" + BitOperator.intToHexString(hex, 2));
        System.out.println("獲取int類(lèi)型0-8位的位數(shù)據(jù):" + BitOperator.intNumberRightBitRangeOperator(999, 0, 8));
        System.out.println("獲取int類(lèi)型9-10位的位數(shù)據(jù):" + BitOperator.intNumberRightBitRangeOperator(999, 8, 10));
        System.out.println("獲取int類(lèi)型10-12位的位數(shù)據(jù):" + BitOperator.intNumberRightBitRangeOperator(256, 10, 12));
        short value = 256;
        System.out.println("獲取short類(lèi)型10-12位的位數(shù)據(jù):" + BitOperator.shortNumberRightBitRangeOperator(value, 8, 12));

        //序列化與反序列化對(duì)象
        byte[] bytes = BitOperator.objToBytes(new A());
        A object = (A) BitOperator.bytesToObject(bytes);
        System.out.println(object);

        //轉(zhuǎn)換int數(shù)據(jù)
        int intV = 12738123;
        byte[] bytes1 = {
            (byte) ((intV >> 24) & 0xff),
            (byte) ((intV >> 16) & 0xff),
            (byte) ((intV >> 8) & 0xff),
            (byte) (intV & 0xff)
        };

        byte[] bytes2 = BitOperator.intNumberToBytes(intV);
        int intNumber = BitOperator.bytesToIntNumber(bytes2);

        //轉(zhuǎn)換short類(lèi)型
        short sv = 999;
        byte[] bytes3 = {
            (byte) ((sv >> 8) & 0xff),
            (byte) (sv & 0xff)
        };
        short i = BitOperator.bytesToShortNumber(bytes3);
        System.out.println(i);
    }

    @ToString
    public static class A implements Serializable {
        public String name = "zs";

    }
}
@SuppressWarnings("all")
public class BitOperator {
    /** DIGITAL */
    public static final String DIGITAL = "0123456789ABCDEF";
    /** 16進(jìn)制表示符 */
    public static final String HEX = "0x";
    /**
     * 數(shù)組轉(zhuǎn)換成 int類(lèi)型
     *
     * @param value value
     * @return the byte [ ]
     * @since 1.0
     */
    public static byte[] intNumberToBytes(int value) {
        return new byte[] {
            byteNumberRightBitRangeOperator(value, 24, 32),
            byteNumberRightBitRangeOperator(value, 16, 24),
            byteNumberRightBitRangeOperator(value, 8, 16),
            byteNumberRightBitRangeOperator(value, 0, 8)
        };
    }

    /**
     * Short number to bytes
     *
     * @param value value
     * @return the byte [ ]
     * @since 1.0
     */
    public static byte[] shortNumberToBytes(short value) {
        return new byte[] {
            (byte) shortNumberRightBitRangeOperator(value, 8, 16),
            (byte) shortNumberRightBitRangeOperator(value, 0, 8)
        };
    }

    /**
     * 數(shù)組轉(zhuǎn)換成int數(shù)據(jù)
     *
     * @param bytes bytes
     * @return the int
     * @since 1.0
     */
    public static int bytesToIntNumber(byte[] bytes) {
        return bytesToNumberValue(bytes,0, (res, value) -> {
            return value | res;
        });
    }

    /**
     * 將數(shù)組轉(zhuǎn)換成short類(lèi)型
     *
     * @param bytes bytes
     * @return the int
     * @since 1.0
     */
    public static short bytesToShortNumber(byte[] bytes) {
        return bytesToNumberValue(bytes, (short) 0, (res, value) -> {
            return (short) (res | value);
        });
    }

    /**
     * Bytes to number value
     *
     * @param <T>      parameter
     * @param bytes    bytes
     * @param value    value
     * @param function function
     * @return the t
     * @since 1.0
     */
    public static <T extends Number> T bytesToNumberValue(byte[] bytes,
                                                          T value,
                                                          BiFunction<Integer, T, T> function) {
        try {
            int tmp = bytes.length * 8;
            for (int i = 0; i < bytes.length; i++) {
                tmp -= 8;
                value = function.apply(((bytes[i] & 0xff) << tmp), value);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;
    }

    /**
     * 16進(jìn)制字符串轉(zhuǎn)換為字節(jié)數(shù)組
     *
     * @param str str
     * @return the byte [ ]
     * @since 1.0
     */
    public static byte[] hexStringToByteArray(String str) {
        int len = str.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(str.charAt(i), 16) << 4)
                                  + Character.digit(str.charAt(i + 1), 16));
        }
        return data;
    }

    /**
     * 字節(jié)數(shù)組轉(zhuǎn)換為16進(jìn)制字符串
     *
     * @param src src
     * @return string string
     * @since 2022.1.1
     */
    public static String bytesArrayToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder();
        if (src == null || src.length <= 0) {
            return null;
        }
        for (byte b : src) {
            int v = b & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

    /**
     * 16進(jìn)制字符串轉(zhuǎn)換為字節(jié)數(shù)組
     *
     * @param hex hex
     * @return byte [ ]
     * @since 1.0
     */
    public static byte[] stringToBytes(String hex) {
        String hex1 = hex.replace(" ", "");
        char[] hex2char = hex1.toCharArray();
        byte[] bytes = new byte[hex1.length() / 2];
        byte temp;
        for (int p = 0; p < bytes.length; p++) {
            temp = (byte) (DIGITAL.indexOf(hex2char[2 * p]) * 16);
            temp += DIGITAL.indexOf(hex2char[2 * p + 1]);
            bytes[p] = (byte) (temp & 0xff);
        }
        return bytes;
    }

    /**
     * string字符串轉(zhuǎn)換為 16進(jìn)制字符串
     *
     * @param hex hex
     * @return the string
     * @since 1.0
     */
    public static String stringToHexString(String s) {
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            int ch = (int) s.charAt(i);
            String s4 = Integer.toHexString(ch);
            str.append(s4);
        }
        return HEX + str.toString();
    }

    /**
     * int類(lèi)型轉(zhuǎn)換為16進(jìn)制字符串
     *
     * @param value  value
     * @param number 不足指定位數(shù)補(bǔ)零
     * @return the string
     * @since 1.0
     */
    public static String intToHexString(int value, int number) {
        return numberValueToHexString(value, number, true);
    }

    /**
     * long類(lèi)型轉(zhuǎn)換為16進(jìn)制字符串
     *
     * @param value  value
     * @param number number
     * @return the string
     * @since 1.0
     */
    public static String longToHexString(long value, int number) {
        return numberValueToHexString(value, number, true);
    }
    /**
     * 數(shù)字類(lèi)型類(lèi)型轉(zhuǎn)換為16進(jìn)制字符串
     *
     * @param value     value
     * @param number    number
     * @param isShowHex 是否拼接Ox
     * @return the string
     * @since 1.0
     */
    public static String numberValueToHexString(Object value, int number, boolean isShowHex) {
        if (number <= 0) {
            number = 2;
        }
        String hex = String.format("%0" + number + "x", value);
        return isShowHex ? HEX + hex : hex;
    }

    /**
     * 獲取指定位的位數(shù)據(jù)
     *
     * @param value     value
     * @param bitNumber bit number
     * @return the int
     * @since 1.0
     */
    public static int intNumberRightBitOperator(int value, int bitNumber) {
        return bitNumberOperator(32, bitNumber, str -> str != null ? ((value & Integer.parseUnsignedInt(str, 2)) >> bitNumber)  : 0);
    }

    /**
     * 獲取Long類(lèi)型指定位數(shù)的數(shù)據(jù)
     *
     * @param value     value
     * @param bitNumber bit number
     * @return the int
     * @since 1.0
     */
    public static Long longNumberRightBitOperator(long value, int bitNumber) {
        return bitNumberOperator(64, bitNumber, str -> str != null ? ((value & Long.parseUnsignedLong(str, 2)) >> bitNumber)  : 0);
    }

    /**
     * 獲取指定位的數(shù)據(jù)
     *
     * @param <T>      parameter
     * @param valueBit 數(shù)據(jù)類(lèi)型的長(zhǎng)度,例如int:32位
     * @param number   表示取多少位的數(shù)據(jù)
     * @param func     func
     * @return the int
     * @since 1.0
     */
    public static <T> T bitNumberOperator(int valueBit, int number, Function<String, T> func) {
        if (number > valueBit) {
            return (T) new Integer(0);
        }
        //獲取到位的長(zhǎng)度,根據(jù)長(zhǎng)度生成對(duì)應(yīng)的二進(jìn)制位數(shù)據(jù),獲取數(shù)據(jù)需要生成 1
        StringBuilder builder = new StringBuilder();
        number = valueBit - number;
        for (int i = 0; i < valueBit; i++) {
            if (i == number) {
                builder.append("1");
            } else {
                builder.append("0");
            }
        }
        return func.apply(builder.toString());
    }
    /**
     * 獲取int類(lèi)型數(shù)據(jù)指定范圍的數(shù)據(jù)
     *
     * @param value value
     * @param start start
     * @param end   end
     * @return the int
     * @since 1.0
     */
    public static int intNumberRightBitRangeOperator(int value, int start, int end) {
        return bitNumberRangeOperator(start, end, 32, str -> str != null ? ((value & Integer.parseUnsignedInt(str, 2)) >> start) : 0);
    }

    /**
     * 獲取int類(lèi)型數(shù)據(jù)指定范圍的數(shù)據(jù)
     *
     * @param value value
     * @param start start
     * @param end   end
     * @return the int
     * @since 1.0
     */
    public static byte byteNumberRightBitRangeOperator(int value, int start, int end) {
        return (byte) intNumberRightBitRangeOperator(value, start, end);
    }

    /**
     * 獲取short類(lèi)型指定范圍位數(shù)的數(shù)據(jù)
     *
     * @param value value
     * @param start start
     * @param end   end
     * @return the int
     * @since 1.0
     */
    public static short shortNumberRightBitRangeOperator(short value, int start, int end) {
        return bitNumberRangeOperator(start, end, 16, str -> (short) (str != null ? ((value  & Short.parseShort(str, 2)) >> start) : 0));
    }

    /**
     * 獲取指定位的數(shù)據(jù)
     *
     * @param <T>        parameter
     * @param start      start
     * @param end        end
     * @param typeLength type length
     * @param func       func
     * @return the int
     * @since 1.0
     */
    public static <T> T bitNumberRangeOperator(int start, int end, int typeLength, Function<String, T> func) {
        if (start > end) {
            return (T) new Integer(0);
        }
        //獲取到位的長(zhǎng)度,根據(jù)長(zhǎng)度生成對(duì)應(yīng)的二進(jìn)制位數(shù)據(jù),獲取數(shù)據(jù)需要生成 1
        StringBuilder builder = new StringBuilder();
        for (int i = typeLength - 1; i >= 0; i--) {
            if (i >= start && i < end) {
                builder.append("1");
            } else {
                builder.append("0");
            }
        }
        return func.apply(builder.toString());
    }


    /**
     * obj對(duì)象轉(zhuǎn)換為字節(jié)數(shù)組
     *
     * @param obj obj
     * @return the string
     * @since 1.0
     */
    public static byte[] objToBytes(Object obj) {
        ByteArrayOutputStream objByteArray = new ByteArrayOutputStream();
        byte[] result;
        try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(objByteArray);) {
            objectOutputStream.writeObject(obj);
            objectOutputStream.flush();
            objectOutputStream.close();
            result = objByteArray.toByteArray();
        } catch (IOException e) {
            throw new RuntimeException("對(duì)象數(shù)據(jù)轉(zhuǎn)換異常:" + e.getMessage());
        }
        return result;
    }

    /**
     * 反序列化對(duì)象
     *
     * @param bytes bytes
     * @return the object
     * @since 1.0
     */
    public static Object bytesToObject(byte[] bytes) {
        ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(bytes);
        Object result = null;
        try(ObjectInputStream objectInputStream = new ObjectInputStream(arrayInputStream);) {
            result = objectInputStream.readObject();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return result;
    }
}

到此這篇關(guān)于Java協(xié)議字節(jié)操作工具類(lèi)詳情的文章就介紹到這了,更多相關(guān)Java操作工具類(lèi)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java WSDL接口webService實(shí)現(xiàn)方式

    java WSDL接口webService實(shí)現(xiàn)方式

    這篇文章主要為大家詳細(xì)介紹了java WSDL接口webService實(shí)現(xiàn)方式的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • Java多線程提交按照時(shí)間順序獲取線程結(jié)果詳解流程

    Java多線程提交按照時(shí)間順序獲取線程結(jié)果詳解流程

    在工作中是否存在這樣的場(chǎng)景,多個(gè)線程提交執(zhí)行,你不想全部線程執(zhí)行結(jié)束了獲取結(jié)果,而是有線程完成返回結(jié)果就獲取消費(fèi)。本文提供該場(chǎng)景的工具類(lèi),可以直接用哦
    2021-11-11
  • Tomcat數(shù)據(jù)源配置方法_JBuilder中

    Tomcat數(shù)據(jù)源配置方法_JBuilder中

    今天幫一同事配置一個(gè)數(shù)據(jù)源,采用tomcat5.5.9,本來(lái)是個(gè)很簡(jiǎn)單的事,以前也配過(guò),但由于很長(zhǎng)時(shí)間沒(méi)用過(guò)容器提供的數(shù)據(jù)源了(IOC用慣了),也只記的個(gè)大概了,所以剛開(kāi)始一配就出錯(cuò)了,google了一下,有很多資料,照著試試卻都不好使(到不是別人說(shuō)的不對(duì),只是大家用的版本不同)。
    2008-10-10
  • Spring Boot中Elasticsearch的連接配置原理與使用詳解

    Spring Boot中Elasticsearch的連接配置原理與使用詳解

    在Spring Boot中,我們可以通過(guò)Elasticsearch實(shí)現(xiàn)對(duì)數(shù)據(jù)的搜索和分析,本文將介紹Spring Boot中Elasticsearch的連接配置、原理和使用方法,感興趣的可以了解一下
    2023-09-09
  • 簡(jiǎn)單實(shí)現(xiàn)Spring的IOC原理詳解

    簡(jiǎn)單實(shí)現(xiàn)Spring的IOC原理詳解

    這篇文章主要介紹了簡(jiǎn)單實(shí)現(xiàn)Spring的IOC原理詳解,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-12-12
  • Java?NIO實(shí)現(xiàn)聊天系統(tǒng)

    Java?NIO實(shí)現(xiàn)聊天系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了Java?NIO實(shí)現(xiàn)聊天系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • Spring Boot 集成Mybatis實(shí)現(xiàn)主從(多數(shù)據(jù)源)分離方案示例

    Spring Boot 集成Mybatis實(shí)現(xiàn)主從(多數(shù)據(jù)源)分離方案示例

    本篇文章主要介紹了Spring Boot 集成Mybatis實(shí)現(xiàn)主從(多數(shù)據(jù)源)分離方案實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下。
    2017-03-03
  • Spring創(chuàng)建IOC容器的方式解析

    Spring創(chuàng)建IOC容器的方式解析

    這篇文章主要介紹了Spring創(chuàng)建IOC容器的方式解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • Spring數(shù)據(jù)庫(kù)連接池實(shí)現(xiàn)原理深入刨析

    Spring數(shù)據(jù)庫(kù)連接池實(shí)現(xiàn)原理深入刨析

    開(kāi)發(fā)web項(xiàng)目,我們肯定會(huì)和數(shù)據(jù)庫(kù)打交道,因此就會(huì)涉及到數(shù)據(jù)庫(kù)鏈接的問(wèn)題。在以前我們開(kāi)發(fā)傳統(tǒng)的SSM結(jié)構(gòu)的項(xiàng)目時(shí)進(jìn)行數(shù)據(jù)庫(kù)鏈接都是通過(guò)JDBC進(jìn)行數(shù)據(jù)鏈接,我們每和數(shù)據(jù)庫(kù)打一次交道都需要先獲取一次鏈接,操作完后再關(guān)閉鏈接,這樣子效率很低,因此就出現(xiàn)了連接池
    2022-11-11
  • 6種SpringBoot中自定義starter的方式介紹

    6種SpringBoot中自定義starter的方式介紹

    在SpringBoot生態(tài)中,starter是一種特殊的依賴(lài),它能夠自動(dòng)裝配相關(guān)組件,簡(jiǎn)化項(xiàng)目配置,本文將詳細(xì)介紹6種不同的自定義starter開(kāi)發(fā)方法,有需要的可以了解下
    2025-04-04

最新評(píng)論

广东省| 天祝| 宣威市| 长泰县| 木里| 丁青县| 汝阳县| 南江县| 全椒县| 延寿县| 永顺县| 富宁县| 宁安市| 鹿邑县| 汉川市| 濮阳市| 六盘水市| 康保县| 商河县| 孝昌县| 县级市| 牡丹江市| 陆良县| 来宾市| 大荔县| 怀化市| 闸北区| 女性| 黄陵县| 和顺县| 深水埗区| 通河县| 山阳县| 乐业县| 汝阳县| 大洼县| 南和县| 青海省| 大安市| 林周县| 鄂州市|