Class BytesUtil

java.lang.Object
com.sure.tool.util.BytesUtil

public class BytesUtil extends Object
字节数组工具类:基础类型与字节互转、拼接、切片、反转、十六进制,零依赖。
Since:
0.2.0
Author:
suretool
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    bytesToInt(byte[] bytes)
    4 字节转 int(大端)。
    static long
    bytesToLong(byte[] bytes)
    8 字节转 long(大端)。
    static short
    bytesToShort(byte[] bytes)
    2 字节转 short(大端)。
    static byte[]
    concat(byte[]... arrays)
    拼接多个字节数组。
    static byte[]
    intToBytes(int value)
    int 转 4 字节(大端)。
    static byte[]
    longToBytes(long value)
    long 转 8 字节(大端)。
    static byte[]
    reverse(byte[] bytes)
    反转字节数组(返回新数组)。
    static byte[]
    shortToBytes(short value)
    short 转 2 字节(大端)。
    static byte[]
    slice(byte[] bytes, int from, int to)
    切片(含 from 不含 to)。
    static byte[]
    字符串转字节数组(UTF-8)。
    static String
    toHex(byte[] bytes)
    转小写十六进制字符串。
    static String
    toString(byte[] bytes)
    字节数组转字符串(UTF-8)。

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • longToBytes

      public static byte[] longToBytes(long value)
      long 转 8 字节(大端)。
      Parameters:
      value - long 值
      Returns:
      8 字节
    • bytesToLong

      public static long bytesToLong(byte[] bytes)
      8 字节转 long(大端)。
      Parameters:
      bytes - 字节(至少 8 字节)
      Returns:
      long 值
    • intToBytes

      public static byte[] intToBytes(int value)
      int 转 4 字节(大端)。
      Parameters:
      value - int 值
      Returns:
      4 字节
    • bytesToInt

      public static int bytesToInt(byte[] bytes)
      4 字节转 int(大端)。
      Parameters:
      bytes - 字节(至少 4 字节)
      Returns:
      int 值
    • shortToBytes

      public static byte[] shortToBytes(short value)
      short 转 2 字节(大端)。
      Parameters:
      value - short 值
      Returns:
      2 字节
    • bytesToShort

      public static short bytesToShort(byte[] bytes)
      2 字节转 short(大端)。
      Parameters:
      bytes - 字节(至少 2 字节)
      Returns:
      short 值
    • concat

      public static byte[] concat(byte[]... arrays)
      拼接多个字节数组。
      Parameters:
      arrays - 数组组
      Returns:
      拼接结果
    • slice

      public static byte[] slice(byte[] bytes, int from, int to)
      切片(含 from 不含 to)。
      Parameters:
      bytes - 原数组
      from - 起始(含)
      to - 结束(不含)
      Returns:
      切片
    • reverse

      public static byte[] reverse(byte[] bytes)
      反转字节数组(返回新数组)。
      Parameters:
      bytes - 原数组
      Returns:
      反转结果
    • toHex

      public static String toHex(byte[] bytes)
      转小写十六进制字符串。
      Parameters:
      bytes - 字节
      Returns:
      十六进制
    • toString

      public static String toString(byte[] bytes)
      字节数组转字符串(UTF-8)。
      Parameters:
      bytes - 字节
      Returns:
      字符串
    • toBytes

      public static byte[] toBytes(String str)
      字符串转字节数组(UTF-8)。
      Parameters:
      str - 字符串
      Returns:
      字节