Skip navigation links
Overview
Package
Class
Use
Tree
Deprecated
Index
Help
hutool
Prev Class
Next Class
Frames
No Frames
All Classes
Summary:
Nested |
Field |
Constr |
Method
Detail:
Field |
Constr |
Method
cn.hutool.core.util
Class ByteUtil
java.lang.Object
cn.hutool.core.util.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]
注:注释来自Hanlp,代码提供来自pr#1492@Github
Since:
5.6.3
Author:
looly, hanlp, FULaBUla
Field Summary
Fields
Modifier and Type
Field and Description
static ByteOrder
CPU_ENDIAN
CPU的字节序
static ByteOrder
DEFAULT_ORDER
Constructor Summary
Constructors
Constructor and Description
ByteUtil()
Method Summary
All Methods Static Methods Concrete Methods
Modifier and Type
Method and Description
static double
bytesToDouble(byte[] bytes)
byte数组转Double
默认以小端序转换
static double
bytesToDouble(byte[] bytes,
ByteOrder byteOrder)
byte数组转double
自定义端序
static float
bytesToFloat(byte[] bytes)
byte数组转float
默认以小端序转换
static float
bytesToFloat(byte[] bytes,
ByteOrder byteOrder)
byte数组转float
自定义端序
static int
bytesToInt(byte[] bytes)
byte[]转int值
默认以小端序转换
static int
bytesToInt(byte[] bytes,
ByteOrder byteOrder)
byte[]转int值
自定义端序
static int
bytesToInt(byte[] bytes,
int start,
ByteOrder byteOrder)
byte[]转int值
自定义端序
static long
bytesToLong(byte[] bytes)
byte数组转long
默认以小端序转换
from: https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java
static long
bytesToLong(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
bytesToLong(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
bytesToNumber(byte[] bytes,
Class targetClass,
ByteOrder byteOrder)
byte数组转换为指定类型数字
static short
bytesToShort(byte[] bytes)
byte数组转short
默认以小端序转换
static short
bytesToShort(byte[] bytes,
ByteOrder byteOrder)
byte数组转short
自定义端序
static short
bytesToShort(byte[] bytes,
int start,
ByteOrder byteOrder)
byte数组转short
自定义端序
static int
byteToUnsignedInt(byte byteValue)
byte转无符号int
static byte[]
doubleToBytes(double doubleValue)
double转byte数组
默认以小端序转换
static byte[]
doubleToBytes(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[]
floatToBytes(float floatValue)
float转byte数组,默认以小端序转换
static byte[]
floatToBytes(float floatValue,
ByteOrder byteOrder)
float转byte数组,自定义端序
static byte
intToByte(int intValue)
int转byte
static byte[]
intToBytes(int intValue)
int转byte数组
默认以小端序转换
static byte[]
intToBytes(int intValue,
ByteOrder byteOrder)
int转byte数组
自定义端序
static byte[]
longToBytes(long longValue)
long转byte数组
默认以小端序转换
from: https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java
static byte[]
longToBytes(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[]
numberToBytes(Number number)
将Number转换为
static byte[]
numberToBytes(Number number,
ByteOrder byteOrder)
将Number转换为
static byte[]
shortToBytes(short shortValue)
short转byte数组
默认以小端序转换
static byte[]
shortToBytes(short shortValue,
ByteOrder byteOrder)
short转byte数组
自定义端序
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Field Detail
DEFAULT_ORDER
public static final ByteOrder DEFAULT_ORDER
CPU_ENDIAN
public static final ByteOrder CPU_ENDIAN
CPU的字节序
Constructor Detail
ByteUtil
public ByteUtil()
Method Detail
intToByte
public static byte intToByte(int intValue)
int转byte
Parameters:
intValue - int值
Returns:
byte值
byteToUnsignedInt
public static int byteToUnsignedInt(byte byteValue)
byte转无符号int
Parameters:
byteValue - byte值
Returns:
无符号int值
Since:
3.2.0
bytesToShort
public static short bytesToShort(byte[] bytes)
byte数组转short
默认以小端序转换
Parameters:
bytes - byte数组
Returns:
short值
bytesToShort
public static short bytesToShort(byte[] bytes,
ByteOrder byteOrder)
byte数组转short
自定义端序
Parameters:
bytes - byte数组,长度必须为2
byteOrder - 端序
Returns:
short值
bytesToShort
public static short bytesToShort(byte[] bytes,
int start,
ByteOrder byteOrder)
byte数组转short
自定义端序
Parameters:
bytes - byte数组,长度必须大于2
start - 开始位置
byteOrder - 端序
Returns:
short值
shortToBytes
public static byte[] shortToBytes(short shortValue)
short转byte数组
默认以小端序转换
Parameters:
shortValue - short值
Returns:
byte数组
shortToBytes
public static byte[] shortToBytes(short shortValue,
ByteOrder byteOrder)
short转byte数组
自定义端序
Parameters:
shortValue - short值
byteOrder - 端序
Returns:
byte数组
bytesToInt
public static int bytesToInt(byte[] bytes)
byte[]转int值
默认以小端序转换
Parameters:
bytes - byte数组
Returns:
int值
bytesToInt
public static int bytesToInt(byte[] bytes,
ByteOrder byteOrder)
byte[]转int值
自定义端序
Parameters:
bytes - byte数组
byteOrder - 端序
Returns:
int值
bytesToInt
public static int bytesToInt(byte[] bytes,
int start,
ByteOrder byteOrder)
byte[]转int值
自定义端序
Parameters:
bytes - byte数组
start - 开始位置(包含)
byteOrder - 端序
Returns:
int值
Since:
5.7.21
intToBytes
public static byte[] intToBytes(int intValue)
int转byte数组
默认以小端序转换
Parameters:
intValue - int值
Returns:
byte数组
intToBytes
public static byte[] intToBytes(int intValue,
ByteOrder byteOrder)
int转byte数组
自定义端序
Parameters:
intValue - int值
byteOrder - 端序
Returns:
byte数组
longToBytes
public static byte[] longToBytes(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数组
longToBytes
public static byte[] longToBytes(long longValue,
ByteOrder byteOrder)
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数组
bytesToLong
public static long bytesToLong(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值
bytesToLong
public static long bytesToLong(byte[] bytes,
ByteOrder byteOrder)
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值
bytesToLong
public static long bytesToLong(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
Parameters:
bytes - byte数组
start - 计算数组开始位置
byteOrder - 端序
Returns:
long值
Since:
5.7.21
floatToBytes
public static byte[] floatToBytes(float floatValue)
float转byte数组,默认以小端序转换
Parameters:
floatValue - float值
Returns:
byte数组
Since:
5.7.18
floatToBytes
public static byte[] floatToBytes(float floatValue,
ByteOrder byteOrder)
float转byte数组,自定义端序
Parameters:
floatValue - float值
byteOrder - 端序
Returns:
byte数组
Since:
5.7.18
bytesToFloat
public static float bytesToFloat(byte[] bytes)
byte数组转float
默认以小端序转换
Parameters:
bytes - byte数组
Returns:
float值
Since:
5.7.18
bytesToFloat
public static float bytesToFloat(byte[] bytes,
ByteOrder byteOrder)
byte数组转float
自定义端序
Parameters:
bytes - byte数组
byteOrder - 端序
Returns:
float值
Since:
5.7.18
doubleToBytes
public static byte[] doubleToBytes(double doubleValue)
double转byte数组
默认以小端序转换
Parameters:
doubleValue - double值
Returns:
byte数组
doubleToBytes
public static byte[] doubleToBytes(double doubleValue,
ByteOrder byteOrder)
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数组
bytesToDouble
public static double bytesToDouble(byte[] bytes)
byte数组转Double
默认以小端序转换
Parameters:
bytes - byte数组
Returns:
long值
bytesToDouble
public static double bytesToDouble(byte[] bytes,
ByteOrder byteOrder)
byte数组转double
自定义端序
Parameters:
bytes - byte数组
byteOrder - 端序
Returns:
long值
numberToBytes
public static byte[] numberToBytes(Number number)
将Number转换为
Parameters:
number - 数字
Returns:
bytes
numberToBytes
public static byte[] numberToBytes(Number number,
ByteOrder byteOrder)
将Number转换为
Parameters:
number - 数字
byteOrder - 端序
Returns:
bytes
bytesToNumber
public static T bytesToNumber(byte[] bytes,
Class targetClass,
ByteOrder byteOrder)
throws IllegalArgumentException
byte数组转换为指定类型数字
Type Parameters:
T - 数字类型
Parameters:
bytes - byte数组
targetClass - 目标数字类型
byteOrder - 端序
Returns:
转换后的数字
Throws:
IllegalArgumentException - 不支持的数字类型,如用户自定义数字类型
Skip navigation links
Overview
Package
Class
Use
Tree
Deprecated
Index
Help
Prev Class
Next Class
Frames
No Frames
All Classes
Summary:
Nested |
Field |
Constr |
Method
Detail:
Field |
Constr |
Method
Copyright © 2023. All rights reserved.
|