Class Hex

java.lang.Object
org.forgerock.util.encode.Hex

public final class Hex extends Object
Routines for encoding and decoding binary data in hexadecimal format.
  • Method Summary

    Modifier and Type
    Method
    Description
    static byte[]
    Decodes a hexadecimal-encoded string into an equivalent byte array, treating each two-character sequence as a single byte in big-endian order.
    static byte[]
    Decodes a hexadecimal-encoded string into an equivalent byte array, treating each two-character sequence as a single byte in big-endian order.
    static String
    encode(byte[] data)
    Encodes binary data into hexadecimal format.
    static String
    encodeLowerCase(byte[] data)
    Encodes binary data into hexadecimal format using lowercase characters for 'a' to 'f'.
    static String
    encodeUpperCase(byte[] data)
    Encodes binary data into hexadecimal format using uppercase characters for 'A' to 'F'.

    Methods inherited from class java.lang.Object

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

    • decode

      public static byte[] decode(String hex)
      Decodes a hexadecimal-encoded string into an equivalent byte array, treating each two-character sequence as a single byte in big-endian order. The input can use either uppercase or lowercase characters, or a combination of both. Any invalid characters in the input will be skipped. Use decodeStrict(String) if you want stricter decoding.
      Parameters:
      hex - the input value in hexadecimal format.
      Returns:
      the equivalent output bytes.
      Throws:
      NullPointerException - if the input is null.
      IllegalArgumentException - if the input length, after stripping invalid characters, is not divisible by 2.
    • decodeStrict

      public static byte[] decodeStrict(String hex)
      Decodes a hexadecimal-encoded string into an equivalent byte array, treating each two-character sequence as a single byte in big-endian order. The input can use either uppercase or lowercase characters, or a combination of both. Any invalid characters in the input will result in an IllegalArgumentException.
      Parameters:
      hex - the input value in hexadecimal format.
      Returns:
      the equivalent output bytes.
      Throws:
      NullPointerException - if the input is null.
      IllegalArgumentException - if any non-hex characters occur in the input or the length is not divisible by two.
    • encodeUpperCase

      public static String encodeUpperCase(byte[] data)
      Encodes binary data into hexadecimal format using uppercase characters for 'A' to 'F'.
      Parameters:
      data - the data to encode.
      Returns:
      the hexadecimal-encoded data.
      Throws:
      NullPointerException - if the input data is null.
    • encodeLowerCase

      public static String encodeLowerCase(byte[] data)
      Encodes binary data into hexadecimal format using lowercase characters for 'a' to 'f'.
      Parameters:
      data - the data to encode.
      Returns:
      the hexadecimal-encoded data.
      Throws:
      NullPointerException - if the input data is null.
    • encode

      public static String encode(byte[] data)
      Encodes binary data into hexadecimal format. This method is equivalent to encodeLowerCase(byte[]).
      Parameters:
      data - the data to encode.
      Returns:
      the hexadecimal-encoded data.
      Throws:
      NullPointerException - if the input data is null.