Package org.miaixz.bus.core.xyz
Class ByteKit
java.lang.Object
org.miaixz.bus.core.xyz.ByteKit
对数字和字节进行转换。
假设数据存储是以大端模式存储的:
- 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 Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Stringbcd(byte[] data, int from, int to) 解析 BCD 码static intbitCount(byte[] buf) 统计byte中位数为1的个数static StringbyteArrayToHexString(byte[] bytes) Parse a byte array into a string of hexadecimal digits including all array bytes as digitsstatic doublebytesToDouble(byte[] data, int off, boolean bigEndian) byte数组转double 默认以:ByteOrder.BIG_ENDIANorByteOrder.LITTLE_ENDIANstatic doublebytesToDoubleBE(byte[] data, int off) byte数组转double 默认以:ByteOrder.BIG_ENDIANstatic doublebytesToDoubleLE(byte[] data, int off) byte数组转double 默认以:ByteOrder.LITTLE_ENDIANstatic floatbytesToFloat(byte[] data, int off, boolean bigEndian) byte数组转float 默认以:ByteOrder.BIG_ENDIANorByteOrder.LITTLE_ENDIANstatic floatbytesToFloatBE(byte[] data, int off) byte数组转float 默认以:ByteOrder.BIG_ENDIANstatic floatbytesToFloatLE(byte[] data, int off) byte数组转float 默认以:ByteOrder.LITTLE_ENDIANstatic intbytesToInt(byte[] data, int off, boolean bigEndian) byte数组转int 默认以:ByteOrder.BIG_ENDIANorByteOrder.LITTLE_ENDIANstatic intbytesToIntBE(byte[] data, int off) byte数组转int 默认以:ByteOrder.BIG_ENDIANstatic intbytesToIntLE(byte[] data, int off) byte数组转int 默认以:ByteOrder.LITTLE_ENDIANstatic longbytesToLong(byte[] data, int off, boolean bigEndian) byte数组转long 默认以:ByteOrder.BIG_ENDIANorByteOrder.LITTLE_ENDIANstatic longbytesToLongBE(byte[] data, int off) byte数组转long 默认以:ByteOrder.BIG_ENDIANstatic longbytesToLongLE(byte[] data, int off) byte数组转long 默认以:ByteOrder.LITTLE_ENDIANstatic intbytesToShort(byte[] data, int off, boolean bigEndian) byte数组转int 默认以:ByteOrder.BIG_ENDIANorByteOrder.LITTLE_ENDIANstatic voidbytesToShort(byte[] data, short[] s, int off, int len, boolean bigEndian) byte数组处理 排序:ByteOrder.BIG_ENDIANorByteOrder.LITTLE_ENDIANstatic intbytesToShortBE(byte[] data, int off) byte数组转int 默认以:ByteOrder.LITTLE_ENDIANstatic intbytesToShortLE(byte[] data, int off) byte数组转int 默认以:ByteOrder.LITTLE_ENDIANstatic voidbytesToShortLE(byte[] data, short[] s, int off, int len) byte数组处理 默认排序:ByteOrder.LITTLE_ENDIANstatic voidbytesToShortsBE(byte[] data, short[] s, int off, int len) byte数组处理 默认排序:ByteOrder.BIG_ENDIANstatic intbytesToTag(byte[] data, int off, boolean bigEndian) byte数组转int 默认以:ByteOrder.BIG_ENDIANorByteOrder.LITTLE_ENDIANstatic intbytesToTagBE(byte[] data, int off) byte数组转int 默认以:ByteOrder.BIG_ENDIANstatic intbytesToTagLE(byte[] data, int off) byte数组转int 默认以:ByteOrder.LITTLE_ENDIANstatic intbytesToUShort(byte[] data, int off, boolean bigEndian) byte数组转int 默认以:ByteOrder.BIG_ENDIANstatic intbytesToUShortBE(byte[] data, int off) byte数组转int 默认以:ByteOrder.LITTLE_ENDIANstatic intbytesToUShortLE(byte[] data, int off) byte数组转int 默认以:ByteOrder.LITTLE_ENDIANstatic intbytesToVR(byte[] data, int off) byte数组转int 默认以:ByteOrder.BIG_ENDIANstatic byte[]concat(byte[]... byteArrays) 连接多个byte[]static byte[]concat(byte[] buf1, byte[] buf2) 将两个字节数组连接到一个新的字节数组static intcountOf(byte[] data, byte target) 统计目标字节在字节数组中出现的次数static byte[]doubleToBytes(double data, byte[] bytes, int off, boolean bigEndian) double转byte数组 排序:ByteOrder.BIG_ENDIANorByteOrder.LITTLE_ENDIANstatic byte[]doubleToBytesBE(double data, byte[] bytes, int off) double转byte数组 默认排序:ByteOrder.BIG_ENDIANstatic byte[]doubleToBytesLE(double data, byte[] bytes, int off) double转byte数组 默认排序:ByteOrder.LITTLE_ENDIANstatic byte[]将long值转为bytes并填充到给定的bytes中static byte[]floatToBytes(float data, byte[] bytes, int off, boolean bigEndian) float转byte数组 排序:ByteOrder.BIG_ENDIANorByteOrder.LITTLE_ENDIANstatic byte[]floatToBytesBE(float data, byte[] bytes, int off) float转byte数组 默认排序:ByteOrder.BIG_ENDIANstatic byte[]floatToBytesLE(float data, byte[] bytes, int off) float转byte数组 默认排序:ByteOrder.LITTLE_ENDIANstatic BigIntegerfromUnsignedByteArray(byte[] buf) 无符号bytes转BigIntegerstatic BigIntegerfromUnsignedByteArray(byte[] buf, int off, int length) 无符号bytes转BigIntegerstatic byte[]hexStringToByteArray(String digits) Parse a string of hexadecimal digits into a byte arraystatic intindexOf(byte[] data, byte target, int from, int to) 寻找目标字节在字节数组中的下标static byte[]intToBytes(int data, byte[] bytes, int off, boolean bigEndian) int转byte数组 排序:ByteOrder.BIG_ENDIANorByteOrder.LITTLE_ENDIANstatic byte[]intToBytesBE(int data, byte[] bytes, int off) int转byte数组 默认排序:ByteOrder.BIG_ENDIANstatic byte[]intToBytesLE(int data, byte[] bytes, int off) int转byte数组 默认排序:ByteOrder.LITTLE_ENDIANstatic byte[]longToBytes(long data, byte[] bytes, int off, boolean bigEndian) long转byte数组 排序:ByteOrder.BIG_ENDIANorByteOrder.LITTLE_ENDIANstatic byte[]longToBytesBE(long data, byte[] bytes, int off) long转byte数组 默认排序:ByteOrder.BIG_ENDIANstatic byte[]longToBytesLE(long data, byte[] bytes, int off) long转byte数组 默认排序:ByteOrder.LITTLE_ENDIANstatic byte[]shortToBytes(int data, byte[] bytes, int off, boolean bigEndian) int转byte数组 排序:ByteOrder.BIG_ENDIANorByteOrder.LITTLE_ENDIANstatic byte[]shortToBytesBE(int data, byte[] bytes, int off) int转byte数组 默认排序:ByteOrder.BIG_ENDIANstatic byte[]shortToBytesLE(int data, byte[] bytes, int off) int转byte数组 默认排序:ByteOrder.LITTLE_ENDIANstatic byte[]swapInts(byte[] data, int off, int len) 字符交换static byte[]swapLongs(byte[] data, int off, int len) 字符交换static byte[][]swapShorts(byte[][] data) 字符交换static byte[]swapShorts(byte[] data, int off, int len) 字符交换static byte[]tagToBytes(int data, byte[] bytes, int off, boolean bigEndian) int转byte数组 排序:ByteOrder.BIG_ENDIANorByteOrder.LITTLE_ENDIANstatic byte[]tagToBytesBE(int data, byte[] bytes, int off) int转byte数组 默认排序:ByteOrder.BIG_ENDIANstatic byte[]tagToBytesLE(int data, byte[] bytes, int off) int转byte数组 默认排序:ByteOrder.LITTLE_ENDIANstatic bytetoByte(int intValue) int转bytestatic byte[]toBytes(char data) char转byte数组static byte[]toBytes(char[] data) char转byte数组static byte[]toBytes(double doubleValue) double转byte数组 默认以小端序转换static byte[]double转byte数组 自定义端序 from: https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-javastatic byte[]toBytes(float floatValue) float转byte数组,默认以小端序转换static byte[]float转byte数组,自定义端序static byte[]toBytes(int intValue) int转byte数组 默认以小端序转换static byte[]int转byte数组 自定义端序static byte[]toBytes(long longValue) long转byte数组 默认以小端序转换 from: https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-javastatic byte[]long转byte数组 自定义端序 from: https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-javastatic byte[]toBytes(short shortValue) short转byte数组 默认以小端序转换static byte[]short转byte数组 自定义端序static byte[]toBytes(CharSequence text) 编码字符串,编码为UTF-8static byte[]toBytes(CharSequence text, Charset charset) 编码字符串static byte[]将Number转换为static byte[]将Number转换为static doubletoDouble(byte[] bytes) byte数组转Double 默认以小端序转换static doublebyte数组转double 自定义端序static floattoFloat(byte[] bytes) byte数组转float 默认以小端序转换static floatbyte数组转float 自定义端序static inttoInt(byte[] bytes) byte[]转int值 默认以小端序转换static intbyte[]转int值 自定义端序static intbyte[]转int值 自定义端序static longtoLong(byte[] bytes) byte数组转long 默认以小端序转换 from: https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-javastatic longbyte数组转long 自定义端序 from: https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-javastatic longbyte数组转long 自定义端序 from: https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-javastatic <T extends Number>
Tbyte数组转换为指定类型数字static shorttoShort(byte[] bytes) byte数组转short 默认以小端序转换static shortbyte数组转short 自定义端序static shortbyte数组转short 自定义端序toUnsignedBitIndex(byte[] bytes) 统计无符号bytes转为bit位数为1的索引集合static byte[]toUnsignedByteArray(int length, BigInteger value) 以无符号字节数组的形式返回传入值。static byte[]toUnsignedByteArray(BigInteger value) 以无符号字节数组的形式返回传入值。static inttoUnsignedInt(byte byteValue) byte转无符号intstatic intunsigned(byte data) 无符号整数static intxor(byte[] data) 异或值,返回
-
Field Details
-
DEFAULT_ORDER
默认字节序:大端在前,小端在后 -
CPU_ENDIAN
CPU的字节序
-
-
Constructor Details
-
ByteKit
public ByteKit()
-
-
Method Details
-
toBytes
编码字符串,编码为UTF-8- Parameters:
text- 字符串- Returns:
- 编码后的字节码
-
toBytes
编码字符串- 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
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
int转byte数组 自定义端序- Parameters:
intValue- int值byteOrder- 端序- Returns:
- byte数组
-
toBytes
public static byte[] toBytes(long longValue) long转byte数组 默认以小端序转换 from: https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java- Parameters:
longValue- long值- Returns:
- byte数组
-
toBytes
long转byte数组 自定义端序 from: https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java- Parameters:
longValue- long值byteOrder- 端序- Returns:
- byte数组
-
fill
将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
float转byte数组,自定义端序- Parameters:
floatValue- float值byteOrder- 端序- Returns:
- byte数组
-
toBytes
public static byte[] toBytes(double doubleValue) double转byte数组 默认以小端序转换- Parameters:
doubleValue- double值- Returns:
- byte数组
-
toBytes
double转byte数组 自定义端序 from: https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java- Parameters:
doubleValue- double值byteOrder- 端序- Returns:
- byte数组
-
toBytes
将Number转换为- Parameters:
number- 数字- Returns:
- bytes
-
toBytes
将Number转换为- Parameters:
number- 数字byteOrder- 端序- Returns:
- bytes
-
toShort
public static short toShort(byte[] bytes) byte数组转short 默认以小端序转换- Parameters:
bytes- byte数组- Returns:
- short值
-
toShort
byte数组转short 自定义端序- Parameters:
bytes- byte数组,长度必须为2byteOrder- 端序- Returns:
- short值
-
toShort
byte数组转short 自定义端序- Parameters:
bytes- byte数组,长度必须大于2start- 开始位置byteOrder- 端序- Returns:
- short值
-
toInt
public static int toInt(byte[] bytes) byte[]转int值 默认以小端序转换- Parameters:
bytes- byte数组- Returns:
- int值
-
toInt
byte[]转int值 自定义端序- Parameters:
bytes- byte数组byteOrder- 端序- Returns:
- int值
-
toInt
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) byte数组转long 默认以小端序转换 from: https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java- Parameters:
bytes- byte数组- Returns:
- long值
-
toLong
byte数组转long 自定义端序 from: https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java- Parameters:
bytes- byte数组byteOrder- 端序- Returns:
- long值
-
toLong
byte数组转long 自定义端序 from: https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java- Parameters:
bytes- byte数组start- 计算数组开始位置byteOrder- 端序- Returns:
- long值
-
toFloat
public static float toFloat(byte[] bytes) byte数组转float 默认以小端序转换- Parameters:
bytes- byte数组- Returns:
- float值
-
toFloat
byte数组转float 自定义端序- Parameters:
bytes- byte数组byteOrder- 端序- Returns:
- float值
-
toDouble
public static double toDouble(byte[] bytes) byte数组转Double 默认以小端序转换- Parameters:
bytes- byte数组- Returns:
- long值
-
toDouble
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
以无符号字节数组的形式返回传入值。- Parameters:
value- 需要转换的值- Returns:
- 无符号bytes
-
toUnsignedByteArray
以无符号字节数组的形式返回传入值。- Parameters:
length- bytes长度value- 需要转换的值- Returns:
- 无符号bytes
-
fromUnsignedByteArray
无符号bytes转BigInteger- Parameters:
buf- buf 无符号bytes- Returns:
BigInteger
-
fromUnsignedByteArray
无符号bytes转BigInteger- Parameters:
buf- 无符号bytesoff- 起始位置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
统计无符号bytes转为bit位数为1的索引集合- Parameters:
bytes- 无符号bytes- Returns:
- 位数为1的索引集合
-
bytesToInt
public static int bytesToInt(byte[] data, int off, boolean bigEndian) byte数组转int 默认以:ByteOrder.BIG_ENDIANorByteOrder.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_ENDIANorByteOrder.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_ENDIANorByteOrder.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_ENDIANorByteOrder.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_ENDIANorByteOrder.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_ENDIANorByteOrder.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_ENDIANorByteOrder.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_ENDIANorByteOrder.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_ENDIANorByteOrder.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_ENDIANorByteOrder.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_ENDIANorByteOrder.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_ENDIANorByteOrder.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_ENDIANorByteOrder.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
解析 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
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
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
-