Class HexUtil

java.lang.Object
de.tomatengames.util.HexUtil

public class HexUtil extends Object
Provides methods to work with hexadecimal strings.
Since:
1.0
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    bytesToHex(byte[] bytes)
    Converts the specified byte array into a hexadecimal string.
    static String
    byteToHex(int value)
    Converts the 8 least significant bits of the specified int into a hexadecimal string.
    static String
    Reads the specified file and returns its content as a hex string.
    static byte[]
    hexToBytes(String hexString)
    Reads the specified hexadecimal string into a byte array.
    static int
    hexToInt(String hexString)
    Reads the specified hexadecimal string into an int.
    static long
    hexToLong(String hexString)
    Reads the specified hexadecimal string into a long.
    static String
    intToHex(int value)
    Converts the bits of the specified int into a hexadecimal string.
    static String
    longToHex(long value)
    Converts the bits of the specified long into a hexadecimal string.
    static int
    parseHexChar(char hexChar)
    Returns the value of the specified hexadecimal character.
    static String
    shortToHex(int value)
    Converts the 16 least significant bits of the specified int into a hexadecimal string.
    static String
    Reads the specified InputStream and returns its content as a hex string.
    static char
    toHexChar(int value)
    Returns the hexadecimal character that represents the specified value.

    Methods inherited from class Object

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

    • toHexChar

      public static char toHexChar(int value)
      Returns the hexadecimal character that represents the specified value.
      Parameters:
      value - The value (0-15).
      Returns:
      The hexadecimal character (0-9, a-f).
      Throws:
      IllegalArgumentException - If the value is out of range.
    • bytesToHex

      public static String bytesToHex(byte[] bytes)
      Converts the specified byte array into a hexadecimal string. Each byte is represented by 2 characters.
      Parameters:
      bytes - The byte array.
      Returns:
      The hexadecimal string. Not null. Alphabetic characters are in lower case.
      Throws:
      NullPointerException - If the byte array is null.
    • longToHex

      public static String longToHex(long value)
      Converts the bits of the specified long into a hexadecimal string. The first character represents the most significant bits.
      Parameters:
      value - The long.
      Returns:
      The hexadecimal string. Not null. The length is always 16. Alphabetic characters are in lower case.
    • intToHex

      public static String intToHex(int value)
      Converts the bits of the specified int into a hexadecimal string. The first character represents the most significant bits.
      Parameters:
      value - The int.
      Returns:
      The hexadecimal string. Not null. The length is always 8. Alphabetic characters are in lower case.
    • byteToHex

      public static String byteToHex(int value)
      Converts the 8 least significant bits of the specified int into a hexadecimal string. The other bits are ignored. The first character of the result represents the most significant bits.
      Parameters:
      value - The int value.
      Returns:
      The hexadecimal string. Not null. The length is always 2. Alphabetic characters are in lower case.
      Since:
      1.6
    • shortToHex

      public static String shortToHex(int value)
      Converts the 16 least significant bits of the specified int into a hexadecimal string. The other bits are ignored. The first character of the result represents the most significant bits.
      Parameters:
      value - The int value.
      Returns:
      The hexadecimal string. Not null. The length is always 4. Alphabetic characters are in lower case.
      Since:
      1.6
    • streamToHex

      public static String streamToHex(InputStream input) throws IOException
      Reads the specified InputStream and returns its content as a hex string. Each byte of the input is represented by 2 characters.
      Parameters:
      input - The input stream.
      Returns:
      The hexadecimal string. Not null. Alphabetic characters are in lower case.
      Throws:
      IOException - If an I/O error occurs.
      NullPointerException - If the input stream is null.
    • fileToHex

      public static String fileToHex(Path path) throws IOException
      Reads the specified file and returns its content as a hex string. Each byte of the file is represented by 2 characters.
      Parameters:
      path - The path to the file.
      Returns:
      The hexadecimal string. Not null. Alphabetic characters are in lower case.
      Throws:
      IOException - If an I/O error occurs.
      NullPointerException - If the path is null.
    • parseHexChar

      public static int parseHexChar(char hexChar)
      Returns the value of the specified hexadecimal character.
      Parameters:
      hexChar - The hexadecimal character (0-9, a-f, A-F).
      Returns:
      The value that the character represents (0-15). It is guaranteed that only the 4 least significant bits may be set.
      Throws:
      IllegalArgumentException - If the character is not a hex character.
    • hexToBytes

      public static byte[] hexToBytes(String hexString)
      Reads the specified hexadecimal string into a byte array.

      Two characters of the string are read into one byte. The first hex char represents the 4 most significant bits and the second hex char the 4 least significant bits of the byte. If the length of the hex string is odd, the 4 least significant bits of the last byte are 0000.

      Parameters:
      hexString - The hexadecimal string.
      Returns:
      The byte array that represents the hexadecimal string. Not null.
      Throws:
      NullPointerException - If the input string is null.
      IllegalArgumentException - If the string contains a non-hexadecimal character.
    • hexToLong

      public static long hexToLong(String hexString)
      Reads the specified hexadecimal string into a long. Each character represents 4 bits of the result. The last character of the string represents the least significant bits.

      Note: A '-' sign is not allowed.

      Parameters:
      hexString - The hexadecimal string. Must not be null. Must not be larger than 16 characters.
      Returns:
      A long that represents the hex string.
      Throws:
      NullPointerException - If the hex string is null.
      IllegalArgumentException - If the hex string is too large or contains non-hexadecimal characters.
    • hexToInt

      public static int hexToInt(String hexString)
      Reads the specified hexadecimal string into an int. Each character represents 4 bits of the result. The last character of the string represents the least significant bits.

      Note: A '-' sign is not allowed.

      Parameters:
      hexString - The hexadecimal string. Must not be null. Must not be larger than 8 characters.
      Returns:
      An int that represents the hex string.
      Throws:
      NullPointerException - If the hex string is null.
      IllegalArgumentException - If the hex string is too large or contains non-hexadecimal characters.
      Since:
      1.1