Class HexUtil


  • public final class HexUtil
    extends java.lang.Object
    Utility class around hex strings.
    Since:
    2.1.0
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean isValid​(java.lang.String hex)
      Checks if a string is formed by an even number of hexadecimal digits.
      static byte toByte​(java.lang.String hex)
      Converts a hexadecimal string to a "byte".
      static byte[] toByteArray​(java.lang.String hex)
      Converts a hexadecimal string to a byte array.
      static java.lang.String toHex​(byte val)
      Converts a "byte" to a hexadecimal string.
      static java.lang.String toHex​(byte[] tab)
      Converts a byte array to a hexadecimal string.
      static java.lang.String toHex​(int val)
      Converts an "integer" to a hexadecimal string.
      static java.lang.String toHex​(long val)
      Converts a "long" to a hexadecimal string.
      static java.lang.String toHex​(short val)
      Converts a "short" to a hexadecimal string.
      static int toInt​(java.lang.String hex)
      Converts a hexadecimal string to an "integer".
      static long toLong​(java.lang.String hex)
      Converts a hexadecimal string to a "long".
      static short toShort​(java.lang.String hex)
      Converts a hexadecimal string to a "short".
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • isValid

        public static boolean isValid​(java.lang.String hex)
        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

        public static byte[] toByteArray​(java.lang.String hex)
        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:
        java.lang.StringIndexOutOfBoundsException - If the input string is made of an odd number of characters.
        Since:
        2.1.0
      • toByte

        public static byte toByte​(java.lang.String hex)
        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

        public static short toShort​(java.lang.String hex)
        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

        public static int toInt​(java.lang.String hex)
        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

        public static long toLong​(java.lang.String hex)
        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

        public static java.lang.String toHex​(byte[] tab)
        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

        public static java.lang.String toHex​(byte val)
        Converts a "byte" to a hexadecimal string.
        Parameters:
        val - The byte to convert.
        Returns:
        A string containing 2 characters.
        Since:
        2.1.0
      • toHex

        public static java.lang.String toHex​(short val)
        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

        public static java.lang.String toHex​(int val)
        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

        public static java.lang.String toHex​(long val)
        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