Class ByteKit

java.lang.Object
org.aoju.bus.core.toolkit.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
  • Constructor Details

    • ByteKit

      public ByteKit()
  • Method Details

    • getByte

      public static byte getByte(int data)
      int转byte
      Parameters:
      data - int值
      Returns:
      the byte
    • getBytes

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

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

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

      public static byte[] getBytes(long data)
      long转byte数组 默认以小端序转换
      Parameters:
      data - long值
      Returns:
      the byte
    • getBytes

      public static byte[] getBytes(long data, ByteOrder byteOrder)
      long转byte数组
      Parameters:
      data - long值
      byteOrder - 端序
      Returns:
      the byte
    • getBytes

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

      public static byte[] getBytes(float data, ByteOrder byteOrder)
      double转byte数组
      Parameters:
      data - double值
      byteOrder - 端序
      Returns:
      the byte
    • getBytes

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

      public static byte[] getBytes(double data, ByteOrder byteOrder)
      double转byte数组
      Parameters:
      data - double值
      byteOrder - 端序
      Returns:
      the byte
    • getBytes

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

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

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

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

      public static byte[] getBytes(String data)
      string转byte数组
      Parameters:
      data - string值
      Returns:
      the byte
    • getBytes

      public static byte[] getBytes(String data, String charset)
      string转byte数组
      Parameters:
      data - string值
      charset - 字符集
      Returns:
      the byte
    • getInt

      public static int getInt(byte data)
      byte转无符号int
      Parameters:
      data - byte值
      Returns:
      the int
    • getInt

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

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

      public static int bytesToInt(byte[] data, int start, ByteOrder byteOrder)
      byte[]转int值
      Parameters:
      data - byte数组
      start - 计算数组开始位置
      byteOrder - 端序
      Returns:
      int值
    • getLong

      public static long getLong(byte[] data)
      byte数组转long
      Parameters:
      data - byte数组
      Returns:
      the long
    • getLong

      public static long getLong(byte[] data, ByteOrder byteOrder)
      byte数组转long
      Parameters:
      data - byte数组
      byteOrder - 端序
      Returns:
      the long
    • bytesToLong

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

      public static float getFloat(byte[] data)
      byte数组转Double 默认以小端序转换
      Parameters:
      data - byte数组
      Returns:
      the float
    • getFloat

      public static float getFloat(byte[] data, ByteOrder byteOrder)
      byte数组转double 自定义端序
      Parameters:
      data - byte数组
      byteOrder - 端序
      Returns:
      the float
    • getDouble

      public static double getDouble(byte[] data)
      byte数组转Double 默认以小端序转换
      Parameters:
      data - byte数组
      Returns:
      the double
    • getDouble

      public static double getDouble(byte[] data, ByteOrder byteOrder)
      byte数组转double 自定义端序
      Parameters:
      data - byte数组
      byteOrder - 端序
      Returns:
      the double
    • getShort

      public static short getShort(byte[] data)
      byte数组转short 默认以小端序转换
      Parameters:
      data - byte数组
      Returns:
      the short
    • getShort

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

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

      public static char getChar(byte[] data)
      byte数组转char
      Parameters:
      data - 字节数组
      Returns:
      the char
    • getString

      public static String getString(byte[] data)
      byte数组转string
      Parameters:
      data - 字节数组
      Returns:
      the string
    • getString

      public static String getString(byte[] data, String charset)
      byte数组转string
      Parameters:
      data - 字节数组
      charset - 字符集
      Returns:
      the string
    • getBytes

      public static byte[] getBytes(Number number)
      Number转换为byte数组
      Parameters:
      number - byte数组
      Returns:
      the byte
    • getNumber

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

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

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

      public static byte[][] split(byte[] data, byte separator)
      根据分隔符拆分字节数组
      Parameters:
      data - 字节数组
      separator - 分隔符
      Returns:
      the byte
    • split

      public static byte[][] split(byte[] data, int len)
      拆分byte数组为几个等份(最后一份可能小于len)
      Parameters:
      data - 数组
      len - 每个小节的长度
      Returns:
      拆分后的数组
    • subArray

      public static byte[] subArray(byte[] data, int from)
      截取子数组
      Parameters:
      data - 字节数组
      from - 开始下标(包含)
      Returns:
      the byte
    • subArray

      public static byte[] subArray(byte[] data, int from, int to)
      截取子数组
      Parameters:
      data - 字节数组
      from - 开始下标(包含)
      to - 结束下标(不包含)
      Returns:
      the byte
    • join

      public static byte[] join(byte data, byte[]... datas)
      连接多个字节数组
      Parameters:
      data - 连字符
      datas - 二维数组
      Returns:
      the byte
    • join

      public static byte[] join(byte[]... data)
      连接多个字节数组
      Parameters:
      data - 二维数组
      Returns:
      the byte
    • trim

      public static byte[] trim(byte[] data, byte empty)
      首尾去掉空字符
      Parameters:
      data - 字节数组
      empty - 空数组
      Returns:
      the byte
    • trim

      public static byte[] trim(byte[] data, byte empty, int from, int to)
      从 from 到 to 截取子串 并 首尾去掉空字符
      Parameters:
      data - 字节数组
      empty - 空字节
      from - 开始下标(包含)
      to - 结束下标(不包含)
      Returns:
      the byte
    • 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)
    • swapLongs

      public static byte[] swapLongs(byte[] data, int off, int len)
    • swapShorts

      public static byte[][] swapShorts(byte[][] data)
    • swapShorts

      public static byte[] swapShorts(byte[] data, int off, int len)
    • 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