public class ByteUtil extends Object
Modifier and Type | Field and Description |
---|---|
static ByteOrder |
CPU_ENDIAN
CPU的字节序
|
static ByteOrder |
DEFAULT_ORDER
默认字节序:大端在前,小端在后
|
Constructor and Description |
---|
ByteUtil() |
Modifier and Type | Method and Description |
---|---|
static int |
bitCount(byte[] buf)
统计byte中位数为1的个数
|
static byte[] |
concat(byte[]... byteArrays)
连接多个byte[]
|
static byte[] |
fill(int intValue,
int start,
ByteOrder byteOrder,
byte[] bytes)
将int值转为bytes并填充到给定的bytes中
|
static byte[] |
fill(long longValue,
int start,
ByteOrder byteOrder,
byte[] bytes)
将long值转为bytes并填充到给定的bytes中
|
static BigInteger |
fromUnsignedByteArray(byte[] buf)
无符号bytes转
BigInteger |
static BigInteger |
fromUnsignedByteArray(byte[] buf,
int off,
int length)
无符号bytes转
BigInteger |
static byte |
toByte(int intValue)
int转byte
|
static byte[] |
toBytes(CharSequence str,
Charset charset)
编码字符串
|
static byte[] |
toBytes(double doubleValue)
double转byte数组
默认以小端序转换 |
static byte[] |
toBytes(double doubleValue,
ByteOrder byteOrder)
double转byte数组
自定义端序 from: https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java |
static byte[] |
toBytes(float floatValue)
float转byte数组,默认以小端序转换
|
static byte[] |
toBytes(float floatValue,
ByteOrder byteOrder)
float转byte数组,自定义端序
|
static byte[] |
toBytes(int intValue)
int转byte数组
默认以小端序转换 |
static byte[] |
toBytes(int intValue,
ByteOrder byteOrder)
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-java |
static byte[] |
toBytes(long longValue,
ByteOrder byteOrder)
long转byte数组
自定义端序 from: https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java |
static byte[] |
toBytes(Number number)
将
Number 转换为 |
static byte[] |
toBytes(Number number,
ByteOrder byteOrder)
将
Number 转换为 |
static byte[] |
toBytes(short shortValue)
short转byte数组
默认以小端序转换 |
static byte[] |
toBytes(short shortValue,
ByteOrder byteOrder)
short转byte数组
自定义端序 |
static double |
toDouble(byte[] bytes)
byte数组转Double
默认以小端序转换 |
static double |
toDouble(byte[] bytes,
ByteOrder byteOrder)
byte数组转double
自定义端序 |
static float |
toFloat(byte[] bytes)
byte数组转float
默认以小端序转换 |
static float |
toFloat(byte[] bytes,
ByteOrder byteOrder)
byte数组转float
自定义端序 |
static int |
toInt(byte[] bytes)
byte[]转int值
默认以小端序转换 |
static int |
toInt(byte[] bytes,
ByteOrder byteOrder)
byte[]转int值
自定义端序 |
static int |
toInt(byte[] bytes,
int start,
ByteOrder byteOrder)
byte[]转int值
自定义端序 |
static long |
toLong(byte[] bytes)
byte数组转long
默认以小端序转换 from: https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java |
static long |
toLong(byte[] bytes,
ByteOrder byteOrder)
byte数组转long
自定义端序 from: https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java |
static long |
toLong(byte[] bytes,
int start,
ByteOrder byteOrder)
byte数组转long
自定义端序 from: https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java |
static <T extends Number> |
toNumber(byte[] bytes,
Class<T> targetClass,
ByteOrder byteOrder)
byte数组转换为指定类型数字
|
static short |
toShort(byte[] bytes)
byte数组转short
默认以小端序转换 |
static short |
toShort(byte[] bytes,
ByteOrder byteOrder)
byte数组转short
自定义端序 |
static short |
toShort(byte[] bytes,
int start,
ByteOrder byteOrder)
byte数组转short
自定义端序 |
static List<Integer> |
toUnsignedBitIndex(byte[] bytes)
统计无符号bytes转为bit位数为1的索引集合
|
static byte[] |
toUnsignedByteArray(BigInteger value)
以无符号字节数组的形式返回传入值。
|
static byte[] |
toUnsignedByteArray(int length,
BigInteger value)
以无符号字节数组的形式返回传入值。
|
static int |
toUnsignedInt(byte byteValue)
byte转无符号int
|
static byte[] |
toUtf8Bytes(CharSequence str)
编码字符串,编码为UTF-8
|
public static final ByteOrder DEFAULT_ORDER
public static final ByteOrder CPU_ENDIAN
public static byte[] toUtf8Bytes(CharSequence str)
str
- 字符串public static byte[] toBytes(CharSequence str, Charset charset)
str
- 字符串charset
- 字符集,如果此字段为空,则解码的结果取决于平台public static byte toByte(int intValue)
intValue
- int值public static byte[] toBytes(short shortValue)
shortValue
- short值public static byte[] toBytes(short shortValue, ByteOrder byteOrder)
shortValue
- short值byteOrder
- 端序public static byte[] toBytes(int intValue)
intValue
- int值public static byte[] toBytes(int intValue, ByteOrder byteOrder)
intValue
- int值byteOrder
- 端序public static byte[] fill(int intValue, int start, ByteOrder byteOrder, byte[] bytes)
intValue
- int值start
- 开始位置(包含)byteOrder
- 端续bytes
- 被填充的bytespublic static byte[] toBytes(long longValue)
longValue
- long值public static byte[] toBytes(long longValue, ByteOrder byteOrder)
longValue
- long值byteOrder
- 端序public static byte[] fill(long longValue, int start, ByteOrder byteOrder, byte[] bytes)
longValue
- long值start
- 开始位置(包含)byteOrder
- 端续bytes
- 被填充的bytespublic static byte[] toBytes(float floatValue)
floatValue
- float值public static byte[] toBytes(float floatValue, ByteOrder byteOrder)
floatValue
- float值byteOrder
- 端序public static byte[] toBytes(double doubleValue)
doubleValue
- double值public static byte[] toBytes(double doubleValue, ByteOrder byteOrder)
doubleValue
- double值byteOrder
- 端序public static byte[] toBytes(Number number)
Number
转换为number
- 数字public static byte[] toBytes(Number number, ByteOrder byteOrder)
Number
转换为number
- 数字byteOrder
- 端序public static short toShort(byte[] bytes)
bytes
- byte数组public static short toShort(byte[] bytes, ByteOrder byteOrder)
bytes
- byte数组,长度必须为2byteOrder
- 端序public static short toShort(byte[] bytes, int start, ByteOrder byteOrder)
bytes
- byte数组,长度必须大于2start
- 开始位置byteOrder
- 端序public static int toInt(byte[] bytes)
bytes
- byte数组public static int toInt(byte[] bytes, ByteOrder byteOrder)
bytes
- byte数组byteOrder
- 端序public static int toInt(byte[] bytes, int start, ByteOrder byteOrder)
bytes
- byte数组start
- 开始位置(包含)byteOrder
- 端序public static int toUnsignedInt(byte byteValue)
byteValue
- byte值public static long toLong(byte[] bytes)
bytes
- byte数组public static long toLong(byte[] bytes, ByteOrder byteOrder)
bytes
- byte数组byteOrder
- 端序public static long toLong(byte[] bytes, int start, ByteOrder byteOrder)
bytes
- byte数组start
- 计算数组开始位置byteOrder
- 端序public static float toFloat(byte[] bytes)
bytes
- byte数组public static float toFloat(byte[] bytes, ByteOrder byteOrder)
bytes
- byte数组byteOrder
- 端序public static double toDouble(byte[] bytes)
bytes
- byte数组public static double toDouble(byte[] bytes, ByteOrder byteOrder)
bytes
- byte数组byteOrder
- 端序public static <T extends Number> T toNumber(byte[] bytes, Class<T> targetClass, ByteOrder byteOrder) throws IllegalArgumentException
T
- 数字类型bytes
- byte数组targetClass
- 目标数字类型byteOrder
- 端序IllegalArgumentException
- 不支持的数字类型,如用户自定义数字类型public static byte[] toUnsignedByteArray(BigInteger value)
value
- 需要转换的值public static byte[] toUnsignedByteArray(int length, BigInteger value)
length
- bytes长度value
- 需要转换的值public static BigInteger fromUnsignedByteArray(byte[] buf)
BigInteger
buf
- buf 无符号bytesBigInteger
public static BigInteger fromUnsignedByteArray(byte[] buf, int off, int length)
BigInteger
buf
- 无符号bytesoff
- 起始位置length
- 长度BigInteger
public static byte[] concat(byte[]... byteArrays)
byteArrays
- 多个byte[]public static int bitCount(byte[] buf)
buf
- 无符号bytesInteger.bitCount(int)
Copyright © 2025. All rights reserved.