public final class ByteArrayUtil
extends java.lang.Object
Modifier and Type | Method and Description |
---|---|
static int |
fourBytesToInt(byte[] bytes,
int offset)
Convert four bytes from a byte array into an integer.
|
static byte[] |
fromHex(java.lang.String hex)
Create a byte array from an hexa string.
|
static int |
threeBytesSignedToInt(byte[] bytes,
int offset)
Convert three bytes (signed) from a byte array into an integer.
|
static int |
threeBytesToInt(byte[] bytes,
int offset)
Convert three bytes (unsigned) from a byte array into an integer.
|
static java.lang.String |
toHex(byte[] byteArray)
Represents the byte array in a hexadecimal string.
|
static int |
twoBytesSignedToInt(byte[] bytes,
int offset)
Convert two bytes (signed) from a byte array into an integer.
|
static int |
twoBytesToInt(byte[] bytes,
int offset)
Convert two bytes (unsigned) from a byte array into an integer.
|
public static byte[] fromHex(java.lang.String hex)
hex
- Hexa stringpublic static java.lang.String toHex(byte[] byteArray)
byteArray
- byte array to represent to hexpublic static int twoBytesToInt(byte[] bytes, int offset)
The two bytes are expected to be in the MSB first order (aka network order).
Throw an exception if the buffer is null or not long enough to contain 2 bytes.
bytes
- byte arrayoffset
- offset from which the 2 bytes arejava.lang.IllegalArgumentException
- if the buffer has a bad lengthpublic static int twoBytesSignedToInt(byte[] bytes, int offset)
The two bytes are expected to be in the MSB first order (aka network order).
The number is also considered as signed. That is, if the MSB (first left bit) is 1, then the number is negative and the conversion is done accordingly with the usual binary arithmetic.
Throw an exception if the buffer is null or not long enough to contain 2 bytes.
bytes
- byte arrayoffset
- offset from which the 2 bytes arejava.lang.IllegalArgumentException
- if the buffer has a bad lengthpublic static int threeBytesToInt(byte[] bytes, int offset)
The three bytes are expected to be in the MSB first order (aka network order).
Throw an exception if the buffer is null or not long enough to contain all 3 bytes.
bytes
- byte arrayoffset
- offset from which the 3 bytes arejava.lang.IllegalArgumentException
- if the buffer has a bad lengthpublic static int threeBytesSignedToInt(byte[] bytes, int offset)
The three bytes are expected to be in the MSB first order (aka network order).
The number is also considered as signed. That is, if the MSB (first left bit) is 1, then the number is negative and the conversion is done accordingly with the usual binary arithmetic.
Throw an exception if the buffer is null or not long enough to contain all 3 bytes.
bytes
- byte array containing a 3-byte signed numberoffset
- offset from which the 3 bytes arejava.lang.IllegalArgumentException
- if the buffer has a bad lengthpublic static int fourBytesToInt(byte[] bytes, int offset)
The four bytes are expected to be in the MSB first order (aka network order).
Throw an exception if the buffer is null or not long enough to contain 4 bytes.
bytes
- byte arrayoffset
- offset from which the 4 bytes arejava.lang.IllegalArgumentException
- if the buffer has a bad length