Class Hex


  • public class Hex
    extends Object
    Utility class for hexadecimal strings
    Author:
    Mike
    • Constructor Summary

      Constructors 
      Constructor Description
      Hex()  
    • Constructor Detail

      • Hex

        public Hex()
    • Method Detail

      • toChar

        public static char toChar​(int value)
        Converts an int value in the range 0..15 to a lowercase hexadecimal character
        Parameters:
        value - Integer value to convert
        Returns:
        The lowercase hex character
        Throws:
        IllegalArgumentException - on invalid hex char
      • toBytes

        public static byte[] toBytes​(String hex)
        Converts a hex string to a byte array. The hex string must have an even number of digits.
        Parameters:
        hex - String containing Hex digits
        Returns:
        The representation of the hex string as a byte array
        Throws:
        Error - on odd length hex string
      • val

        public static int val​(char c)
        Gets the value of a single hex car e.g. val('c') => 12
        Parameters:
        c - Character to convert
        Returns:
        The int value of the hex char, in the range 0-15 inclusive
        Throws:
        Error - on invalid hex char
      • toString

        public static String toString​(byte[] data)
        Converts a byte array of length N to a hex string of length 2N
        Parameters:
        data - Array of bytes
        Returns:
        The lowercase hex string generated
      • toString

        public static String toString​(byte[] data,
                                      int offset,
                                      int length)
        Converts a byte array of length N to a hex string of length 2N
        Parameters:
        data - Array of bytes
        offset - Offset into the byte array to start
        length - Number of bytes from the byte array to convert to hex
        Returns:
        The lowercase hex string generated
      • toString

        public static String toString​(int value)
        Converts an int to its 8-character hex representation
        Parameters:
        value - int to convert to hex
        Returns:
        hex representation