Class HexUtil
- Since:
- 2.1.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
Checks if a string is formed by an even number of hexadecimal digits.static byte
Converts a hexadecimal string to a "byte".static byte[]
toByteArray
(String hex) Converts a hexadecimal string to a byte array.static String
toHex
(byte val) Converts a "byte" to a hexadecimal string.static String
toHex
(byte[] tab) Converts a byte array to a hexadecimal string.static String
toHex
(int val) Converts an "integer" to a hexadecimal string.static String
toHex
(long val) Converts a "long" to a hexadecimal string.static String
toHex
(short val) Converts a "short" to a hexadecimal string.static int
Converts a hexadecimal string to an "integer".static long
Converts a hexadecimal string to a "long".static short
Converts a hexadecimal string to a "short".
-
Method Details
-
isValid
Checks if a string is formed by an even number of hexadecimal digits."1234AB"
will match."1234AB2"
,"12 34AB"
or"x1234AB"
won't match.
- Parameters:
hex
- The string to check.- Returns:
- True if the string matches the expected hexadecimal representation, false otherwise.
- Since:
- 2.1.0
-
toByteArray
Converts a hexadecimal string to a byte array.Caution: the result may be erroneous if the string does not contain only hexadecimal characters.
- Parameters:
hex
- The hexadecimal string to convert.- Returns:
- An empty byte array if the input string is null or empty.
- Throws:
StringIndexOutOfBoundsException
- If the input string is made of an odd number of characters.- Since:
- 2.1.0
-
toByte
Converts a hexadecimal string to a "byte".Note: if the hexadecimal string contains more than two characters, then only the last two characters will be taken into account. In this case, please note that the conversion processing will be less performant.
Caution: the result may be erroneous if the string does not contain only hexadecimal characters.
- Parameters:
hex
- The hexadecimal string to convert.- Returns:
- 0 if the input string is null or empty.
- Since:
- 2.1.0
-
toShort
Converts a hexadecimal string to a "short".Note: if the hexadecimal string contains more than four characters, then only the last four characters will be taken into account. In this case, please note that the conversion processing will be less performant.
Caution: the result may be erroneous if the string does not contain only hexadecimal characters.
- Parameters:
hex
- The hexadecimal string to convert.- Returns:
- 0 if the input string is null or empty.
- Since:
- 2.1.0
-
toInt
Converts a hexadecimal string to an "integer".Note: if the hexadecimal string contains more than eight characters, then only the last eight characters will be taken into account. In this case, please note that the conversion processing will be less performant.
Caution: the result may be erroneous if the string does not contain only hexadecimal characters.
- Parameters:
hex
- The hexadecimal string to convert.- Returns:
- 0 if the input string is null or empty.
- Since:
- 2.1.0
-
toLong
Converts a hexadecimal string to a "long".Note: if the hexadecimal string contains more than sixteen characters, then only the last sixteen characters will be taken into account. In this case, please note that the conversion processing will be less performant.
Caution: the result may be erroneous if the string does not contain only hexadecimal characters.
- Parameters:
hex
- The hexadecimal string to convert.- Returns:
- 0 if the input string is null or empty.
- Since:
- 2.1.0
-
toHex
Converts a byte array to a hexadecimal string.- Parameters:
tab
- The byte array to convert.- Returns:
- A string with a size equal to (2 * size of the input array).
- Since:
- 2.1.0
-
toHex
Converts a "byte" to a hexadecimal string.- Parameters:
val
- The byte to convert.- Returns:
- A string containing 2 characters.
- Since:
- 2.1.0
-
toHex
Converts a "short" to a hexadecimal string.Note: the returned string has an even length and is left truncated if necessary to keep only the significant characters.
- Parameters:
val
- The short to convert.- Returns:
- A string containing 2 or 4 characters.
- Since:
- 2.1.0
-
toHex
Converts an "integer" to a hexadecimal string.Note: the returned string has an even length and is left truncated if necessary to keep only the significant characters.
- Parameters:
val
- The integer to convert.- Returns:
- A string containing 2, 4, 6 or 8 characters.
- Since:
- 2.1.0
-
toHex
Converts a "long" to a hexadecimal string.Note: the returned string has an even length and is left truncated if necessary to keep only the significant characters.
- Parameters:
val
- The long to convert.- Returns:
- A string containing 2, 4, 6, 8, 10, 12, 14 or 16 characters.
- Since:
- 2.1.0
-