Class ByteArrayUtil
- java.lang.Object
-
- org.eclipse.keyple.core.util.ByteArrayUtil
-
public final class ByteArrayUtil extends java.lang.Object
Utils around byte arrays- Since:
- 2.0.0
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static int
fourBytesToInt(byte[] bytes, int offset)
Converts 4 bytes located at the offset provided in the byte array into an unsigned integer.static byte[]
fromHex(java.lang.String hex)
Create a byte array from an hexadecimal string made of consecutive even number of digits in the range {0..9,a..f,A..F}.static boolean
isValidHexString(java.lang.String hexString)
Checks if the provided string is formed by an even number of hexadecimal digits.static java.lang.String
normalizeHexString(java.lang.String hexString)
Normalizes the input hex string by padding on the left by a zero if necessary.static int
threeBytesSignedToInt(byte[] bytes, int offset)
Converts 3 bytes located at the offset provided in the byte array into an signed integer.static int
threeBytesToInt(byte[] bytes, int offset)
Converts 3 bytes located at the offset provided in the byte array into an unsigned integer.static java.lang.String
toHex(byte[] byteArray)
Represents the byte array in a hexadecimal string.static int
twoBytesSignedToInt(byte[] bytes, int offset)
Converts 2 bytes located at the offset provided in the byte array into an signed integer.static int
twoBytesToInt(byte[] bytes, int offset)
Converts 2 bytes located at the offset provided in the byte array into an unsigned integer.
-
-
-
Method Detail
-
isValidHexString
public static boolean isValidHexString(java.lang.String hexString)
Checks if the provided string is formed by an even number of hexadecimal digits.
"1234AB"
will match."1234AB2"
,"12 34AB"
or"x1234AB"
won't match.
- Parameters:
hexString
- A string.- Returns:
- true if the string matches the expected hexadecimal representation, false otherwise.
-
normalizeHexString
public static java.lang.String normalizeHexString(java.lang.String hexString)
Normalizes the input hex string by padding on the left by a zero if necessary.- Parameters:
hexString
- The hex string to normalize.- Returns:
- A not null string.
- Throws:
java.lang.NullPointerException
- If the input string is null.- Since:
- 2.0.0
-
fromHex
public static byte[] fromHex(java.lang.String hex)
Create a byte array from an hexadecimal string made of consecutive even number of digits in the range {0..9,a..f,A..F}.No checks are performed on the input string, except for nullity, zero length and length parity.
- Parameters:
hex
- An hexadecimal string.- Returns:
- A reference of not empty of byte array.
- Throws:
java.lang.IllegalArgumentException
- If the provided string is null, empty or made of an odd number of characters.- Since:
- 2.0.0
- See Also:
isValidHexString(String)
-
toHex
public static java.lang.String toHex(byte[] byteArray)
Represents the byte array in a hexadecimal string.- Parameters:
byteArray
- The byte array to represent in hexadecimal.- Returns:
- An hexadecimal string representation of byteArray, an empty string of if the byte array is null.
- Since:
- 2.0.0
-
twoBytesToInt
public static int twoBytesToInt(byte[] bytes, int offset)
Converts 2 bytes located at the offset provided in the byte array into an unsigned integer.The 2 bytes are assumed to be in the of the most significant byte first order (aka 'network order' or 'big-endian' or 'MSB').
- Parameters:
bytes
- A byte array.offset
- The position of the 2 bytes in the array.- Returns:
- A positive int.
- Throws:
java.lang.IllegalArgumentException
- If the buffer has a bad length or the offset is negative.- Since:
- 2.0.0
-
twoBytesSignedToInt
public static int twoBytesSignedToInt(byte[] bytes, int offset)
Converts 2 bytes located at the offset provided in the byte array into an signed integer.The 2 bytes are assumed to be in the of the most significant byte first order (aka 'network order' or 'big-endian' or 'MSB').
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.
- Parameters:
bytes
- A byte array.offset
- The position of the 2 bytes in the array.- Returns:
- A negative or positive int.
- Throws:
java.lang.IllegalArgumentException
- If the buffer has a bad length or the offset is negative.- Since:
- 2.0.0
-
threeBytesToInt
public static int threeBytesToInt(byte[] bytes, int offset)
Converts 3 bytes located at the offset provided in the byte array into an unsigned integer.The 3 bytes are assumed to be in the of the most significant byte first order (aka 'network order' or 'big-endian' or 'MSB').
- Parameters:
bytes
- A byte array.offset
- The position of the 3 bytes in the array.- Returns:
- A positive int.
- Throws:
java.lang.IllegalArgumentException
- if the buffer has a bad length- Since:
- 2.0.0
-
threeBytesSignedToInt
public static int threeBytesSignedToInt(byte[] bytes, int offset)
Converts 3 bytes located at the offset provided in the byte array into an signed integer.The 3 bytes are assumed to be in the of the most significant byte first order (aka 'network order' or 'big-endian' or 'MSB').
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.
- Parameters:
bytes
- A byte array.offset
- The position of the 3 bytes in the array.- Returns:
- A positive int.
- Throws:
java.lang.IllegalArgumentException
- if the buffer has a bad length- Since:
- 2.0.0
-
fourBytesToInt
public static int fourBytesToInt(byte[] bytes, int offset)
Converts 4 bytes located at the offset provided in the byte array into an unsigned integer.The 4 bytes are assumed to be in the of the most significant byte first order (aka 'network order' or 'big-endian' or 'MSB').
- Parameters:
bytes
- A byte array.offset
- The position of the 4 bytes in the array.- Returns:
- A positive int.
- Throws:
java.lang.IllegalArgumentException
- if the buffer has a bad length- Since:
- 2.0.0
-
-