Class ByteKit

java.lang.Object
org.miaixz.bus.core.xyz.ByteKit

public class ByteKit 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]
Since:
Java 17+
Author:
Kimi Liu
  • Field Details

    • DEFAULT_ORDER

      public static final ByteOrder DEFAULT_ORDER
      默认字节序:大端在前,小端在后
    • CPU_ENDIAN

      public static final ByteOrder CPU_ENDIAN
      CPU的字节序
  • Constructor Details

    • ByteKit

      public ByteKit()
  • Method Details

    • toBytes

      public static byte[] toBytes(CharSequence text)
      编码字符串,编码为UTF-8
      Parameters:
      text - 字符串
      Returns:
      编码后的字节码
    • toBytes

      public static byte[] toBytes(CharSequence text, Charset charset)
      编码字符串
      Parameters:
      text - 字符串
      charset - 字符集,如果此字段为空,则解码的结果取决于平台
      Returns:
      编码后的字节码
    • toByte

      public static byte toByte(int intValue)
      int转byte
      Parameters:
      intValue - int值
      Returns:
      byte值
    • toBytes

      public static byte[] toBytes(short shortValue)
      short转byte数组 默认以小端序转换
      Parameters:
      shortValue - short值
      Returns:
      byte数组
    • toBytes

      public static byte[] toBytes(short shortValue, ByteOrder byteOrder)
      short转byte数组 自定义端序
      Parameters:
      shortValue - short值
      byteOrder - 端序
      Returns:
      byte数组
    • toBytes

      public static byte[] toBytes(char data)
      char转byte数组
      Parameters:
      data - char值
      Returns:
      the byte
    • toBytes

      public static byte[] toBytes(char[] data)
      char转byte数组
      Parameters:
      data - char值
      Returns:
      the byte
    • toBytes

      public static byte[] toBytes(int intValue)
      int转byte数组 默认以小端序转换
      Parameters:
      intValue - int值
      Returns:
      byte数组
    • toBytes

      public static byte[] toBytes(int intValue, ByteOrder byteOrder)
      int转byte数组 自定义端序
      Parameters:
      intValue - int值
      byteOrder - 端序
      Returns:
      byte数组
    • toBytes

      public static byte[] toBytes(long longValue)
      Parameters:
      longValue - long值
      Returns:
      byte数组
    • toBytes

      public static byte[] toBytes(long longValue, ByteOrder byteOrder)
      Parameters:
      longValue - long值
      byteOrder - 端序
      Returns:
      byte数组
    • fill

      public static byte[] fill(long longValue, int start, ByteOrder byteOrder, byte[] bytes)
      将long值转为bytes并填充到给定的bytes中
      Parameters:
      longValue - long值
      start - 开始位置(包含)
      byteOrder - 端续
      bytes - 被填充的bytes
      Returns:
      填充后的bytes
    • toBytes

      public static byte[] toBytes(float floatValue)
      float转byte数组,默认以小端序转换
      Parameters:
      floatValue - float值
      Returns:
      byte数组
    • toBytes

      public static byte[] toBytes(float floatValue, ByteOrder byteOrder)
      float转byte数组,自定义端序
      Parameters:
      floatValue - float值
      byteOrder - 端序
      Returns:
      byte数组
    • toBytes

      public static byte[] toBytes(double doubleValue)
      double转byte数组 默认以小端序转换
      Parameters:
      doubleValue - double值
      Returns:
      byte数组
    • toBytes

      public static byte[] toBytes(double doubleValue, ByteOrder byteOrder)
      Parameters:
      doubleValue - double值
      byteOrder - 端序
      Returns:
      byte数组
    • toBytes

      public static byte[] toBytes(Number number)
      Number转换为
      Parameters:
      number - 数字
      Returns:
      bytes
    • toBytes

      public static byte[] toBytes(Number number, ByteOrder byteOrder)
      Number转换为
      Parameters:
      number - 数字
      byteOrder - 端序
      Returns:
      bytes
    • toShort

      public static short toShort(byte[] bytes)
      byte数组转short 默认以小端序转换
      Parameters:
      bytes - byte数组
      Returns:
      short值
    • toShort

      public static short toShort(byte[] bytes, ByteOrder byteOrder)
      byte数组转short 自定义端序
      Parameters:
      bytes - byte数组,长度必须为2
      byteOrder - 端序
      Returns:
      short值
    • toShort

      public static short toShort(byte[] bytes, int start, ByteOrder byteOrder)
      byte数组转short 自定义端序
      Parameters:
      bytes - byte数组,长度必须大于2
      start - 开始位置
      byteOrder - 端序
      Returns:
      short值
    • toInt

      public static int toInt(byte[] bytes)
      byte[]转int值 默认以小端序转换
      Parameters:
      bytes - byte数组
      Returns:
      int值
    • toInt

      public static int toInt(byte[] bytes, ByteOrder byteOrder)
      byte[]转int值 自定义端序
      Parameters:
      bytes - byte数组
      byteOrder - 端序
      Returns:
      int值
    • toInt

      public static int toInt(byte[] bytes, int start, ByteOrder byteOrder)
      byte[]转int值 自定义端序
      Parameters:
      bytes - byte数组
      start - 开始位置(包含)
      byteOrder - 端序
      Returns:
      int值
    • toUnsignedInt

      public static int toUnsignedInt(byte byteValue)
      byte转无符号int
      Parameters:
      byteValue - byte值
      Returns:
      无符号int值
    • toLong

      public static long toLong(byte[] bytes)
      Parameters:
      bytes - byte数组
      Returns:
      long值
    • toLong

      public static long toLong(byte[] bytes, ByteOrder byteOrder)
      Parameters:
      bytes - byte数组
      byteOrder - 端序
      Returns:
      long值
    • toLong

      public static long toLong(byte[] bytes, int start, ByteOrder byteOrder)
      Parameters:
      bytes - byte数组
      start - 计算数组开始位置
      byteOrder - 端序
      Returns:
      long值
    • toFloat

      public static float toFloat(byte[] bytes)
      byte数组转float 默认以小端序转换
      Parameters:
      bytes - byte数组
      Returns:
      float值
    • toFloat

      public static float toFloat(byte[] bytes, ByteOrder byteOrder)
      byte数组转float 自定义端序
      Parameters:
      bytes - byte数组
      byteOrder - 端序
      Returns:
      float值
    • toDouble

      public static double toDouble(byte[] bytes)
      byte数组转Double 默认以小端序转换
      Parameters:
      bytes - byte数组
      Returns:
      long值
    • toDouble

      public static double toDouble(byte[] bytes, ByteOrder byteOrder)
      byte数组转double 自定义端序
      Parameters:
      bytes - byte数组
      byteOrder - 端序
      Returns:
      long值
    • toNumber

      public static <T extends Number> T toNumber(byte[] bytes, Class<T> targetClass, ByteOrder byteOrder) throws IllegalArgumentException
      byte数组转换为指定类型数字
      Type Parameters:
      T - 数字类型
      Parameters:
      bytes - byte数组
      targetClass - 目标数字类型
      byteOrder - 端序
      Returns:
      转换后的数字
      Throws:
      IllegalArgumentException - 不支持的数字类型,如用户自定义数字类型
    • toUnsignedByteArray

      public static byte[] toUnsignedByteArray(BigInteger value)
      以无符号字节数组的形式返回传入值。
      Parameters:
      value - 需要转换的值
      Returns:
      无符号bytes
    • toUnsignedByteArray

      public static byte[] toUnsignedByteArray(int length, BigInteger value)
      以无符号字节数组的形式返回传入值。
      Parameters:
      length - bytes长度
      value - 需要转换的值
      Returns:
      无符号bytes
    • fromUnsignedByteArray

      public static BigInteger fromUnsignedByteArray(byte[] buf)
      无符号bytes转BigInteger
      Parameters:
      buf - buf 无符号bytes
      Returns:
      BigInteger
    • fromUnsignedByteArray

      public static BigInteger fromUnsignedByteArray(byte[] buf, int off, int length)
      无符号bytes转BigInteger
      Parameters:
      buf - 无符号bytes
      off - 起始位置
      length - 长度
      Returns:
      BigInteger
    • concat

      public static byte[] concat(byte[]... byteArrays)
      连接多个byte[]
      Parameters:
      byteArrays - 多个byte[]
      Returns:
      连接后的byte[]
    • bitCount

      public static int bitCount(byte[] buf)
      统计byte中位数为1的个数
      Parameters:
      buf - 无符号bytes
      Returns:
      为 1 的个数
      See Also:
    • toUnsignedBitIndex

      public static List<Integer> toUnsignedBitIndex(byte[] bytes)
      统计无符号bytes转为bit位数为1的索引集合
      Parameters:
      bytes - 无符号bytes
      Returns:
      位数为1的索引集合
    • bytesToInt

      public static int bytesToInt(byte[] data, int off, boolean bigEndian)
      byte数组转int 默认以: ByteOrder.BIG_ENDIAN or ByteOrder.LITTLE_ENDIAN
      Parameters:
      data - byte数组
      off - 偏移量
      bigEndian - 是否大字节序列
      Returns:
      the int
    • bytesToIntBE

      public static int bytesToIntBE(byte[] data, int off)
      byte数组转int 默认以: ByteOrder.BIG_ENDIAN
      Parameters:
      data - byte数组
      off - 偏移量
      Returns:
      the int
    • bytesToIntLE

      public static int bytesToIntLE(byte[] data, int off)
      byte数组转int 默认以: ByteOrder.LITTLE_ENDIAN
      Parameters:
      data - byte数组
      off - 偏移量
      Returns:
      the int
    • bytesToShort

      public static int bytesToShort(byte[] data, int off, boolean bigEndian)
      byte数组转int 默认以: ByteOrder.BIG_ENDIAN or ByteOrder.LITTLE_ENDIAN
      Parameters:
      data - byte数组
      off - 偏移量
      bigEndian - 是否大字节序列
      Returns:
      the int
    • bytesToShort

      public static void bytesToShort(byte[] data, short[] s, int off, int len, boolean bigEndian)
      byte数组处理 排序: ByteOrder.BIG_ENDIAN or ByteOrder.LITTLE_ENDIAN
      Parameters:
      data - long值
      s - 字符
      off - 偏移量
      len - 字符长度
      bigEndian - 是否大字节序列
    • bytesToShortBE

      public static int bytesToShortBE(byte[] data, int off)
      byte数组转int 默认以: ByteOrder.LITTLE_ENDIAN
      Parameters:
      data - byte数组
      off - 偏移量
      Returns:
      the int
    • bytesToShortLE

      public static int bytesToShortLE(byte[] data, int off)
      byte数组转int 默认以: ByteOrder.LITTLE_ENDIAN
      Parameters:
      data - byte数组
      off - 偏移量
      Returns:
      the int
    • bytesToShortsBE

      public static void bytesToShortsBE(byte[] data, short[] s, int off, int len)
      byte数组处理 默认排序: ByteOrder.BIG_ENDIAN
      Parameters:
      data - double值
      s - 字符
      off - 偏移量
      len - 字符长度
    • bytesToShortLE

      public static void bytesToShortLE(byte[] data, short[] s, int off, int len)
      byte数组处理 默认排序: ByteOrder.LITTLE_ENDIAN
      Parameters:
      data - double值
      s - 字符
      off - 偏移量
      len - 字符长度
    • bytesToUShort

      public static int bytesToUShort(byte[] data, int off, boolean bigEndian)
      byte数组转int 默认以: ByteOrder.BIG_ENDIAN
      Parameters:
      data - byte数组
      off - 偏移量
      bigEndian - 是否大字节序列
      Returns:
      the int
    • bytesToUShortBE

      public static int bytesToUShortBE(byte[] data, int off)
      byte数组转int 默认以: ByteOrder.LITTLE_ENDIAN
      Parameters:
      data - byte数组
      off - 偏移量
      Returns:
      the int
    • bytesToUShortLE

      public static int bytesToUShortLE(byte[] data, int off)
      byte数组转int 默认以: ByteOrder.LITTLE_ENDIAN
      Parameters:
      data - byte数组
      off - 偏移量
      Returns:
      the int
    • bytesToFloat

      public static float bytesToFloat(byte[] data, int off, boolean bigEndian)
      byte数组转float 默认以: ByteOrder.BIG_ENDIAN or ByteOrder.LITTLE_ENDIAN
      Parameters:
      data - byte数组
      off - 偏移量
      bigEndian - 是否大字节序列
      Returns:
      the float
    • bytesToFloatBE

      public static float bytesToFloatBE(byte[] data, int off)
      byte数组转float 默认以: ByteOrder.BIG_ENDIAN
      Parameters:
      data - byte数组
      off - 偏移量
      Returns:
      the float
    • bytesToFloatLE

      public static float bytesToFloatLE(byte[] data, int off)
      byte数组转float 默认以: ByteOrder.LITTLE_ENDIAN
      Parameters:
      data - byte数组
      off - 偏移量
      Returns:
      the float
    • bytesToLong

      public static long bytesToLong(byte[] data, int off, boolean bigEndian)
      byte数组转long 默认以: ByteOrder.BIG_ENDIAN or ByteOrder.LITTLE_ENDIAN
      Parameters:
      data - byte数组
      off - 偏移量
      bigEndian - 是否大字节序列
      Returns:
      the long
    • bytesToLongBE

      public static long bytesToLongBE(byte[] data, int off)
      byte数组转long 默认以: ByteOrder.BIG_ENDIAN
      Parameters:
      data - byte数组
      off - 偏移量
      Returns:
      the long
    • bytesToLongLE

      public static long bytesToLongLE(byte[] data, int off)
      byte数组转long 默认以: ByteOrder.LITTLE_ENDIAN
      Parameters:
      data - byte数组
      off - 偏移量
      Returns:
      the long
    • bytesToDouble

      public static double bytesToDouble(byte[] data, int off, boolean bigEndian)
      byte数组转double 默认以: ByteOrder.BIG_ENDIAN or ByteOrder.LITTLE_ENDIAN
      Parameters:
      data - byte数组
      off - 偏移量
      bigEndian - 是否大字节序列
      Returns:
      the double
    • bytesToDoubleBE

      public static double bytesToDoubleBE(byte[] data, int off)
      byte数组转double 默认以: ByteOrder.BIG_ENDIAN
      Parameters:
      data - byte数组
      off - 偏移量
      Returns:
      the double
    • bytesToDoubleLE

      public static double bytesToDoubleLE(byte[] data, int off)
      byte数组转double 默认以: ByteOrder.LITTLE_ENDIAN
      Parameters:
      data - byte数组
      off - 偏移量
      Returns:
      the double
    • bytesToVR

      public static int bytesToVR(byte[] data, int off)
      byte数组转int 默认以: ByteOrder.BIG_ENDIAN
      Parameters:
      data - byte数组
      off - 偏移量
      Returns:
      the int
    • bytesToTag

      public static int bytesToTag(byte[] data, int off, boolean bigEndian)
      byte数组转int 默认以: ByteOrder.BIG_ENDIAN or ByteOrder.LITTLE_ENDIAN
      Parameters:
      data - byte数组
      off - 偏移量
      bigEndian - 是否大字节序列
      Returns:
      the int
    • bytesToTagBE

      public static int bytesToTagBE(byte[] data, int off)
      byte数组转int 默认以: ByteOrder.BIG_ENDIAN
      Parameters:
      data - byte数组
      off - 偏移量
      Returns:
      the int
    • bytesToTagLE

      public static int bytesToTagLE(byte[] data, int off)
      byte数组转int 默认以: ByteOrder.LITTLE_ENDIAN
      Parameters:
      data - byte数组
      off - 偏移量
      Returns:
      the int
    • intToBytes

      public static byte[] intToBytes(int data, byte[] bytes, int off, boolean bigEndian)
      int转byte数组 排序: ByteOrder.BIG_ENDIAN or ByteOrder.LITTLE_ENDIAN
      Parameters:
      data - float值
      bytes - 目标字节
      off - 偏移量
      bigEndian - 是否大字节序列
      Returns:
      the byte
    • intToBytesBE

      public static byte[] intToBytesBE(int data, byte[] bytes, int off)
      int转byte数组 默认排序: ByteOrder.BIG_ENDIAN
      Parameters:
      data - int值
      bytes - 目标字节
      off - 偏移量
      Returns:
      the byte
    • intToBytesLE

      public static byte[] intToBytesLE(int data, byte[] bytes, int off)
      int转byte数组 默认排序: ByteOrder.LITTLE_ENDIAN
      Parameters:
      data - int值
      bytes - 目标字节
      off - 偏移量
      Returns:
      the byte
    • shortToBytes

      public static byte[] shortToBytes(int data, byte[] bytes, int off, boolean bigEndian)
      int转byte数组 排序: ByteOrder.BIG_ENDIAN or ByteOrder.LITTLE_ENDIAN
      Parameters:
      data - int值
      bytes - 目标字节
      off - 偏移量
      bigEndian - 是否大字节序列
      Returns:
      the byte
    • shortToBytesBE

      public static byte[] shortToBytesBE(int data, byte[] bytes, int off)
      int转byte数组 默认排序: ByteOrder.BIG_ENDIAN
      Parameters:
      data - int值
      bytes - 目标字节
      off - 偏移量
      Returns:
      the byte
    • shortToBytesLE

      public static byte[] shortToBytesLE(int data, byte[] bytes, int off)
      int转byte数组 默认排序: ByteOrder.LITTLE_ENDIAN
      Parameters:
      data - int值
      bytes - 目标字节
      off - 偏移量
      Returns:
      the byte
    • longToBytes

      public static byte[] longToBytes(long data, byte[] bytes, int off, boolean bigEndian)
      long转byte数组 排序: ByteOrder.BIG_ENDIAN or ByteOrder.LITTLE_ENDIAN
      Parameters:
      data - long值
      bytes - 目标字节
      off - 偏移量
      bigEndian - 是否大字节序列
      Returns:
      the byte
    • longToBytesBE

      public static byte[] longToBytesBE(long data, byte[] bytes, int off)
      long转byte数组 默认排序: ByteOrder.BIG_ENDIAN
      Parameters:
      data - long值
      bytes - 目标字节
      off - 偏移量
      Returns:
      the byte
    • longToBytesLE

      public static byte[] longToBytesLE(long data, byte[] bytes, int off)
      long转byte数组 默认排序: ByteOrder.LITTLE_ENDIAN
      Parameters:
      data - long值
      bytes - 目标字节
      off - 偏移量
      Returns:
      the byte
    • floatToBytes

      public static byte[] floatToBytes(float data, byte[] bytes, int off, boolean bigEndian)
      float转byte数组 排序: ByteOrder.BIG_ENDIAN or ByteOrder.LITTLE_ENDIAN
      Parameters:
      data - float值
      bytes - 目标字节
      off - 偏移量
      bigEndian - 是否大字节序列
      Returns:
      the byte
    • floatToBytesBE

      public static byte[] floatToBytesBE(float data, byte[] bytes, int off)
      float转byte数组 默认排序: ByteOrder.BIG_ENDIAN
      Parameters:
      data - double值
      bytes - 目标字节
      off - 偏移量
      Returns:
      the byte
    • floatToBytesLE

      public static byte[] floatToBytesLE(float data, byte[] bytes, int off)
      float转byte数组 默认排序: ByteOrder.LITTLE_ENDIAN
      Parameters:
      data - double值
      bytes - 目标字节
      off - 偏移量
      Returns:
      the byte
    • doubleToBytes

      public static byte[] doubleToBytes(double data, byte[] bytes, int off, boolean bigEndian)
      double转byte数组 排序: ByteOrder.BIG_ENDIAN or ByteOrder.LITTLE_ENDIAN
      Parameters:
      data - double值
      bytes - 目标字节
      off - 偏移量
      bigEndian - 是否大字节序列
      Returns:
      the byte
    • doubleToBytesBE

      public static byte[] doubleToBytesBE(double data, byte[] bytes, int off)
      double转byte数组 默认排序: ByteOrder.BIG_ENDIAN
      Parameters:
      data - double值
      bytes - 目标字节
      off - 偏移量
      Returns:
      the byte
    • doubleToBytesLE

      public static byte[] doubleToBytesLE(double data, byte[] bytes, int off)
      double转byte数组 默认排序: ByteOrder.LITTLE_ENDIAN
      Parameters:
      data - double值
      bytes - 目标字节
      off - 偏移量
      Returns:
      the byte
    • tagToBytes

      public static byte[] tagToBytes(int data, byte[] bytes, int off, boolean bigEndian)
      int转byte数组 排序: ByteOrder.BIG_ENDIAN or ByteOrder.LITTLE_ENDIAN
      Parameters:
      data - float值
      bytes - 目标字节
      off - 偏移量
      bigEndian - 是否大字节序列
      Returns:
      the byte
    • tagToBytesBE

      public static byte[] tagToBytesBE(int data, byte[] bytes, int off)
      int转byte数组 默认排序: ByteOrder.BIG_ENDIAN
      Parameters:
      data - int值
      bytes - 目标字节
      off - 偏移量
      Returns:
      the byte
    • tagToBytesLE

      public static byte[] tagToBytesLE(int data, byte[] bytes, int off)
      int转byte数组 默认排序: ByteOrder.LITTLE_ENDIAN
      Parameters:
      data - int值
      bytes - 目标字节
      off - 偏移量
      Returns:
      the byte
    • swapInts

      public static byte[] swapInts(byte[] data, int off, int len)
      字符交换
      Parameters:
      data - byte值
      off - 偏移量
      len - 长度
      Returns:
      the byte
    • swapLongs

      public static byte[] swapLongs(byte[] data, int off, int len)
      字符交换
      Parameters:
      data - byte值
      off - 偏移量
      len - 长度
      Returns:
      the byte
    • swapShorts

      public static byte[][] swapShorts(byte[][] data)
      字符交换
      Parameters:
      data - byte值
      Returns:
      the byte
    • swapShorts

      public static byte[] swapShorts(byte[] data, int off, int len)
      字符交换
      Parameters:
      data - byte值
      off - 偏移量
      len - 长度
      Returns:
      the byte
    • indexOf

      public static int indexOf(byte[] data, byte target, int from, int to)
      寻找目标字节在字节数组中的下标
      Parameters:
      data - 字节数组
      target - 目标字节
      from - 检索开始下标(包含)
      to - 检索结束下标(不包含)
      Returns:
      找不到则返回-1
    • countOf

      public static int countOf(byte[] data, byte target)
      统计目标字节在字节数组中出现的次数
      Parameters:
      data - 字节数组
      target - 目标字节
      Returns:
      the int
    • bcd

      public static String bcd(byte[] data, int from, int to)
      解析 BCD 码
      Parameters:
      data - 字节数组
      from - 开始下标(包含)
      to - 结束下标(不包含)
      Returns:
      the string
    • unsigned

      public static int unsigned(byte data)
      无符号整数
      Parameters:
      data - 字节
      Returns:
      the int
    • xor

      public static int xor(byte[] data)
      异或值,返回
      Parameters:
      data - 数组
      Returns:
      异或值
    • concat

      public static byte[] concat(byte[] buf1, byte[] buf2)
      将两个字节数组连接到一个新的字节数组
      Parameters:
      buf1 - 字节数组
      buf2 - 字节数组
      Returns:
      the byte
    • byteArrayToHexString

      public static String byteArrayToHexString(byte[] bytes)
      Parse a byte array into a string of hexadecimal digits including all array bytes as digits
      Parameters:
      bytes - The byte array to represent
      Returns:
      A string of hex characters corresponding to the bytes. The string is upper case.
    • hexStringToByteArray

      public static byte[] hexStringToByteArray(String digits)
      Parse a string of hexadecimal digits into a byte array
      Parameters:
      digits - The string to be parsed
      Returns:
      a byte array with each pair of characters converted to a byte, or empty array if the string is not valid hex