Class HexUtil

java.lang.Object
org.eclipse.keyple.core.util.HexUtil

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

    Modifier and Type
    Method
    Description
    static 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[]
    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".

    Methods inherited from class java.lang.Object

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

    • isValid

      public static boolean isValid(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(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:
      StringIndexOutOfBoundsException - If the input string is made of an odd number of characters.
      Since:
      2.1.0
    • toByte

      public static byte toByte(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(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(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(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 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 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 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 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 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