类 ByteUtil
- java.lang.Object
-
- cc.shacocloud.mirage.utils.ByteUtil
-
public class ByteUtil 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]
- 作者:
- 思追(shaco)
-
-
字段概要
字段 修饰符和类型 字段 说明 static ByteOrderCPU_ENDIANCPU的字节序static ByteOrderDEFAULT_ORDER
-
构造器概要
构造器 构造器 说明 ByteUtil()
-
方法概要
所有方法 静态方法 具体方法 修饰符和类型 方法 说明 static doublebytesToDouble(byte[] bytes)byte数组转Double
默认以小端序转换static doublebytesToDouble(byte[] bytes, ByteOrder byteOrder)byte数组转double
自定义端序static doublebytesToFloat(byte[] bytes)byte数组转float
默认以小端序转换static floatbytesToFloat(byte[] bytes, ByteOrder byteOrder)byte数组转float
自定义端序static intbytesToInt(byte[] bytes)byte[]转int值
默认以小端序转换static intbytesToInt(byte[] bytes, int start, ByteOrder byteOrder)byte[]转int值
自定义端序static intbytesToInt(byte[] bytes, ByteOrder byteOrder)byte[]转int值
自定义端序static longbytesToLong(byte[] bytes)byte数组转long
默认以小端序转换static longbytesToLong(byte[] bytes, int start, ByteOrder byteOrder)byte数组转long
自定义端序static longbytesToLong(byte[] bytes, ByteOrder byteOrder)byte数组转long
自定义端序static <T extends Number>
TbytesToNumber(byte[] bytes, Class<T> targetClass, ByteOrder byteOrder)byte数组转换为指定类型数字static shortbytesToShort(byte[] bytes)byte数组转short
默认以小端序转换static shortbytesToShort(byte[] bytes, int start, ByteOrder byteOrder)byte数组转short
自定义端序static shortbytesToShort(byte[] bytes, ByteOrder byteOrder)byte数组转short
自定义端序static intbyteToUnsignedInt(byte byteValue)byte转无符号intstatic byte @NotNull []doubleToBytes(double doubleValue)double转byte数组
默认以小端序转换static byte @NotNull []doubleToBytes(double doubleValue, ByteOrder byteOrder)double转byte数组
自定义端序static byte @NotNull []floatToBytes(float floatValue)float转byte数组,默认以小端序转换static byte @NotNull []floatToBytes(float floatValue, ByteOrder byteOrder)float转byte数组,自定义端序static byteintToByte(int intValue)int转bytestatic byte @NotNull []intToBytes(int intValue)int转byte数组
默认以小端序转换static byte @NotNull []intToBytes(int intValue, ByteOrder byteOrder)int转byte数组
自定义端序static byte @NotNull []longToBytes(long longValue)long转byte数组
默认以小端序转换static byte @NotNull []longToBytes(long longValue, ByteOrder byteOrder)long转byte数组
自定义端序static byte[]numberToBytes(Number number)将Number转换为static byte[]numberToBytes(Number number, ByteOrder byteOrder)将Number转换为static byte @NotNull []shortToBytes(short shortValue)short转byte数组
默认以小端序转换static byte @NotNull []shortToBytes(short shortValue, ByteOrder byteOrder)short转byte数组
自定义端序
-
-
-
方法详细资料
-
intToByte
public static byte intToByte(int intValue)
int转byte- 参数:
intValue- int值- 返回:
- byte值
-
byteToUnsignedInt
public static int byteToUnsignedInt(byte byteValue)
byte转无符号int- 参数:
byteValue- byte值- 返回:
- 无符号int值
-
bytesToShort
public static short bytesToShort(byte[] bytes)
byte数组转short
默认以小端序转换- 参数:
bytes- byte数组- 返回:
- short值
-
bytesToShort
public static short bytesToShort(byte[] bytes, ByteOrder byteOrder)byte数组转short
自定义端序- 参数:
bytes- byte数组,长度必须为2byteOrder- 端序- 返回:
- short值
-
bytesToShort
public static short bytesToShort(byte[] bytes, int start, ByteOrder byteOrder)byte数组转short
自定义端序- 参数:
bytes- byte数组,长度必须大于2start- 开始位置byteOrder- 端序- 返回:
- short值
-
shortToBytes
@Contract(pure=true) public static byte @NotNull [] shortToBytes(short shortValue)
short转byte数组
默认以小端序转换- 参数:
shortValue- short值- 返回:
- byte数组
-
shortToBytes
@Contract(pure=true) public static byte @NotNull [] shortToBytes(short shortValue, ByteOrder byteOrder)short转byte数组
自定义端序- 参数:
shortValue- short值byteOrder- 端序- 返回:
- byte数组
-
bytesToInt
public static int bytesToInt(byte[] bytes)
byte[]转int值
默认以小端序转换- 参数:
bytes- byte数组- 返回:
- int值
-
bytesToInt
public static int bytesToInt(byte[] bytes, ByteOrder byteOrder)byte[]转int值
自定义端序- 参数:
bytes- byte数组byteOrder- 端序- 返回:
- int值
-
bytesToInt
public static int bytesToInt(byte[] bytes, int start, ByteOrder byteOrder)byte[]转int值
自定义端序- 参数:
bytes- byte数组start- 开始位置(包含)byteOrder- 端序- 返回:
- int值
-
intToBytes
@Contract(value="_ -> new", pure=true) public static byte @NotNull [] intToBytes(int intValue)int转byte数组
默认以小端序转换- 参数:
intValue- int值- 返回:
- byte数组
-
intToBytes
@Contract(value="_, _ -> new", pure=true) public static byte @NotNull [] intToBytes(int intValue, ByteOrder byteOrder)int转byte数组
自定义端序- 参数:
intValue- int值byteOrder- 端序- 返回:
- byte数组
-
longToBytes
@Contract(pure=true) public static byte @NotNull [] longToBytes(long longValue)
long转byte数组
默认以小端序转换- 参数:
longValue- long值- 返回:
- byte数组
- 另请参阅:
- 参考
-
longToBytes
@Contract(pure=true) public static byte @NotNull [] longToBytes(long longValue, ByteOrder byteOrder)long转byte数组
自定义端序- 参数:
longValue- long值byteOrder- 端序- 返回:
- byte数组
- 另请参阅:
- 参考
-
bytesToLong
public static long bytesToLong(byte[] bytes)
byte数组转long
默认以小端序转换- 参数:
bytes- byte数组- 返回:
- long值
- 另请参阅:
- 参考
-
bytesToLong
public static long bytesToLong(byte[] bytes, ByteOrder byteOrder)byte数组转long
自定义端序- 参数:
bytes- byte数组byteOrder- 端序- 返回:
- long值
- 另请参阅:
- 参考
-
bytesToLong
public static long bytesToLong(byte[] bytes, int start, ByteOrder byteOrder)byte数组转long
自定义端序- 参数:
bytes- byte数组start- 计算数组开始位置byteOrder- 端序- 返回:
- long值
- 另请参阅:
- 参考
-
floatToBytes
@Contract("_ -> new") public static byte @NotNull [] floatToBytes(float floatValue)float转byte数组,默认以小端序转换- 参数:
floatValue- float值- 返回:
- byte数组
-
floatToBytes
@Contract("_, _ -> new") public static byte @NotNull [] floatToBytes(float floatValue, ByteOrder byteOrder)float转byte数组,自定义端序- 参数:
floatValue- float值byteOrder- 端序- 返回:
- byte数组
-
bytesToFloat
public static double bytesToFloat(byte[] bytes)
byte数组转float
默认以小端序转换- 参数:
bytes- byte数组- 返回:
- float值
-
bytesToFloat
public static float bytesToFloat(byte[] bytes, ByteOrder byteOrder)byte数组转float
自定义端序- 参数:
bytes- byte数组byteOrder- 端序- 返回:
- float值
-
doubleToBytes
public static byte @NotNull [] doubleToBytes(double doubleValue)
double转byte数组
默认以小端序转换- 参数:
doubleValue- double值- 返回:
- byte数组
-
doubleToBytes
public static byte @NotNull [] doubleToBytes(double doubleValue, ByteOrder byteOrder)double转byte数组
自定义端序- 参数:
doubleValue- double值byteOrder- 端序- 返回:
- byte数组
- 另请参阅:
- 参考
-
bytesToDouble
public static double bytesToDouble(byte[] bytes)
byte数组转Double
默认以小端序转换- 参数:
bytes- byte数组- 返回:
- long值
-
bytesToDouble
public static double bytesToDouble(byte[] bytes, ByteOrder byteOrder)byte数组转double
自定义端序- 参数:
bytes- byte数组byteOrder- 端序- 返回:
- long值
-
numberToBytes
public static byte[] numberToBytes(Number number)
将Number转换为- 参数:
number- 数字- 返回:
- bytes
-
numberToBytes
public static byte[] numberToBytes(Number number, ByteOrder byteOrder)
将Number转换为- 参数:
number- 数字byteOrder- 端序- 返回:
- bytes
-
bytesToNumber
public static <T extends Number> T bytesToNumber(byte[] bytes, Class<T> targetClass, ByteOrder byteOrder) throws IllegalArgumentException
byte数组转换为指定类型数字- 类型参数:
T- 数字类型- 参数:
bytes- byte数组targetClass- 目标数字类型byteOrder- 端序- 返回:
- 转换后的数字
- 抛出:
IllegalArgumentException- 不支持的数字类型,如用户自定义数字类型
-
-