类 ByteUtil


  • public class ByteUtil
    extends Object
    对数字和字节进行转换。

    假设数据存储是以大端模式存储的:

    • byte: 字节类型 占8位二进制 00000000
    • char: 字符类型 占2个字节 16位二进制 byte[0] byte[1]
    • int : 整数类型 占4个字节 32位二进制 byte[0] byte[1] byte[2] byte[3]
    • long: 长整数类型 占8个字节 64位二进制 byte[0] byte[1] byte[2] byte[3] byte[4] byte[5]
    • long: 长整数类型 占8个字节 64位二进制 byte[0] byte[1] byte[2] byte[3] byte[4] byte[5] byte[6] byte[7]
    • float: 浮点数(小数) 占4个字节 32位二进制 byte[0] byte[1] byte[2] byte[3]
    • double: 双精度浮点数(小数) 占8个字节 64位二进制 byte[0] byte[1] byte[2] byte[3] byte[4]byte[5] byte[6] byte[7]
    作者:
    思追(shaco)
    • 字段详细资料

      • DEFAULT_ORDER

        public static final ByteOrder DEFAULT_ORDER
      • CPU_ENDIAN

        public static final ByteOrder CPU_ENDIAN
        CPU的字节序
    • 构造器详细资料

      • ByteUtil

        public ByteUtil()
    • 方法详细资料

      • intToByte

        public static byte intToByte​(int intValue)
        int转byte
        参数:
        intValue - int值
        返回:
        byte值
      • byteToUnsignedInt

        public static int byteToUnsignedInt​(byte byteValue)
        byte转无符号int
        参数:
        byteValue - byte值
        返回:
        无符号int值
      • bytesToShort

        public static short bytesToShort​(byte[] bytes)
        byte数组转short
        默认以小端序转换
        参数:
        bytes - byte数组
        返回:
        short值
      • bytesToShort

        public static short bytesToShort​(byte[] bytes,
                                         ByteOrder byteOrder)
        byte数组转short
        自定义端序
        参数:
        bytes - byte数组,长度必须为2
        byteOrder - 端序
        返回:
        short值
      • bytesToShort

        public static short bytesToShort​(byte[] bytes,
                                         int start,
                                         ByteOrder byteOrder)
        byte数组转short
        自定义端序
        参数:
        bytes - byte数组,长度必须大于2
        start - 开始位置
        byteOrder - 端序
        返回:
        short值
      • shortToBytes

        @Contract(pure=true)
        public static byte @NotNull [] shortToBytes​(short shortValue)
        short转byte数组
        默认以小端序转换
        参数:
        shortValue - short值
        返回:
        byte数组
      • shortToBytes

        @Contract(pure=true)
        public static byte @NotNull [] shortToBytes​(short shortValue,
                                                    ByteOrder byteOrder)
        short转byte数组
        自定义端序
        参数:
        shortValue - short值
        byteOrder - 端序
        返回:
        byte数组
      • bytesToInt

        public static int bytesToInt​(byte[] bytes)
        byte[]转int值
        默认以小端序转换
        参数:
        bytes - byte数组
        返回:
        int值
      • bytesToInt

        public static int bytesToInt​(byte[] bytes,
                                     ByteOrder byteOrder)
        byte[]转int值
        自定义端序
        参数:
        bytes - byte数组
        byteOrder - 端序
        返回:
        int值
      • bytesToInt

        public static int bytesToInt​(byte[] bytes,
                                     int start,
                                     ByteOrder byteOrder)
        byte[]转int值
        自定义端序
        参数:
        bytes - byte数组
        start - 开始位置(包含)
        byteOrder - 端序
        返回:
        int值
      • intToBytes

        @Contract(value="_ -> new",
                  pure=true)
        public static byte @NotNull [] intToBytes​(int intValue)
        int转byte数组
        默认以小端序转换
        参数:
        intValue - int值
        返回:
        byte数组
      • intToBytes

        @Contract(value="_, _ -> new",
                  pure=true)
        public static byte @NotNull [] intToBytes​(int intValue,
                                                  ByteOrder byteOrder)
        int转byte数组
        自定义端序
        参数:
        intValue - int值
        byteOrder - 端序
        返回:
        byte数组
      • longToBytes

        @Contract(pure=true)
        public static byte @NotNull [] longToBytes​(long longValue)
        long转byte数组
        默认以小端序转换
        参数:
        longValue - long值
        返回:
        byte数组
        另请参阅:
        参考
      • longToBytes

        @Contract(pure=true)
        public static byte @NotNull [] longToBytes​(long longValue,
                                                   ByteOrder byteOrder)
        long转byte数组
        自定义端序
        参数:
        longValue - long值
        byteOrder - 端序
        返回:
        byte数组
        另请参阅:
        参考
      • bytesToLong

        public static long bytesToLong​(byte[] bytes)
        byte数组转long
        默认以小端序转换
        参数:
        bytes - byte数组
        返回:
        long值
        另请参阅:
        参考
      • bytesToLong

        public static long bytesToLong​(byte[] bytes,
                                       ByteOrder byteOrder)
        byte数组转long
        自定义端序
        参数:
        bytes - byte数组
        byteOrder - 端序
        返回:
        long值
        另请参阅:
        参考
      • bytesToLong

        public static long bytesToLong​(byte[] bytes,
                                       int start,
                                       ByteOrder byteOrder)
        byte数组转long
        自定义端序
        参数:
        bytes - byte数组
        start - 计算数组开始位置
        byteOrder - 端序
        返回:
        long值
        另请参阅:
        参考
      • floatToBytes

        @Contract("_ -> new")
        public static byte @NotNull [] floatToBytes​(float floatValue)
        float转byte数组,默认以小端序转换
        参数:
        floatValue - float值
        返回:
        byte数组
      • floatToBytes

        @Contract("_, _ -> new")
        public static byte @NotNull [] floatToBytes​(float floatValue,
                                                    ByteOrder byteOrder)
        float转byte数组,自定义端序
        参数:
        floatValue - float值
        byteOrder - 端序
        返回:
        byte数组
      • bytesToFloat

        public static double bytesToFloat​(byte[] bytes)
        byte数组转float
        默认以小端序转换
        参数:
        bytes - byte数组
        返回:
        float值
      • bytesToFloat

        public static float bytesToFloat​(byte[] bytes,
                                         ByteOrder byteOrder)
        byte数组转float
        自定义端序
        参数:
        bytes - byte数组
        byteOrder - 端序
        返回:
        float值
      • doubleToBytes

        public static byte @NotNull [] doubleToBytes​(double doubleValue)
        double转byte数组
        默认以小端序转换
        参数:
        doubleValue - double值
        返回:
        byte数组
      • doubleToBytes

        public static byte @NotNull [] doubleToBytes​(double doubleValue,
                                                     ByteOrder byteOrder)
        double转byte数组
        自定义端序
        参数:
        doubleValue - double值
        byteOrder - 端序
        返回:
        byte数组
        另请参阅:
        参考
      • bytesToDouble

        public static double bytesToDouble​(byte[] bytes)
        byte数组转Double
        默认以小端序转换
        参数:
        bytes - byte数组
        返回:
        long值
      • bytesToDouble

        public static double bytesToDouble​(byte[] bytes,
                                           ByteOrder byteOrder)
        byte数组转double
        自定义端序
        参数:
        bytes - byte数组
        byteOrder - 端序
        返回:
        long值
      • numberToBytes

        public static byte[] numberToBytes​(Number number)
        Number转换为
        参数:
        number - 数字
        返回:
        bytes
      • numberToBytes

        public static byte[] numberToBytes​(Number number,
                                           ByteOrder byteOrder)
        Number转换为
        参数:
        number - 数字
        byteOrder - 端序
        返回:
        bytes
      • bytesToNumber

        public static <T extends Number> T bytesToNumber​(byte[] bytes,
                                                         Class<T> targetClass,
                                                         ByteOrder byteOrder)
                                                  throws IllegalArgumentException
        byte数组转换为指定类型数字
        类型参数:
        T - 数字类型
        参数:
        bytes - byte数组
        targetClass - 目标数字类型
        byteOrder - 端序
        返回:
        转换后的数字
        抛出:
        IllegalArgumentException - 不支持的数字类型,如用户自定义数字类型