Package org.forgerock.util.encode
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.COMMONS-1182: Remove and replace several methods with JDK 17's HexFormat once commons is compiled with Java 17.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description 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.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.static Stringencode(byte[] data)Encodes binary data into hexadecimal format.static StringencodeLowerCase(byte[] data)Encodes binary data into hexadecimal format using lowercase characters for 'a' to 'f'.static StringencodeUpperCase(byte[] data)Encodes binary data into hexadecimal format using uppercase characters for 'A' to 'F'.
-
-
-
Method Detail
-
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. UsedecodeStrict(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 anIllegalArgumentException.- 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 toencodeLowerCase(byte[]).- Parameters:
data- the data to encode.- Returns:
- the hexadecimal-encoded data.
- Throws:
NullPointerException- if the input data is null.
-
-