Converts hexadecimal Strings. The Charset used for certain operation can be set, the default is set in #DEFAULT_CHARSET_NAME This class is thread-safe. @since 1.1
| 37 | * @since 1.1 |
| 38 | */ |
| 39 | public class Hex implements BinaryEncoder, BinaryDecoder { |
| 40 | |
| 41 | /** |
| 42 | * Default charset is {@link StandardCharsets#UTF_8} |
| 43 | * |
| 44 | * @since 1.7 |
| 45 | */ |
| 46 | public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8; |
| 47 | |
| 48 | /** |
| 49 | * Default charset name is {@link CharEncoding#UTF_8} |
| 50 | * |
| 51 | * @since 1.4 |
| 52 | */ |
| 53 | public static final String DEFAULT_CHARSET_NAME = CharEncoding.UTF_8; |
| 54 | |
| 55 | /** |
| 56 | * Used to build output as Hex |
| 57 | */ |
| 58 | private static final char[] DIGITS_LOWER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', |
| 59 | 'e', 'f' }; |
| 60 | |
| 61 | /** |
| 62 | * Used to build output as Hex |
| 63 | */ |
| 64 | private static final char[] DIGITS_UPPER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', |
| 65 | 'E', 'F' }; |
| 66 | |
| 67 | /** |
| 68 | * Converts a String representing hexadecimal values into an array of bytes of those same values. The returned array |
| 69 | * will be half the length of the passed String, as it takes two characters to represent any given byte. An |
| 70 | * exception is thrown if the passed String has an odd number of elements. |
| 71 | * |
| 72 | * @param data A String containing hexadecimal digits |
| 73 | * @return A byte array containing binary data decoded from the supplied char array. |
| 74 | * @throws DecoderException Thrown if an odd number or illegal of characters is supplied |
| 75 | * @since 1.11 |
| 76 | */ |
| 77 | public static byte[] decodeHex(final String data) throws DecoderException { |
| 78 | return decodeHex(data.toCharArray()); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Converts an array of characters representing hexadecimal values into an array of bytes of those same values. The |
| 83 | * returned array will be half the length of the passed array, as it takes two characters to represent any given |
| 84 | * byte. An exception is thrown if the passed char array has an odd number of elements. |
| 85 | * |
| 86 | * @param data An array of characters containing hexadecimal digits |
| 87 | * @return A byte array containing binary data decoded from the supplied char array. |
| 88 | * @throws DecoderException Thrown if an odd number or illegal of characters is supplied |
| 89 | */ |
| 90 | public static byte[] decodeHex(final char[] data) throws DecoderException { |
| 91 | |
| 92 | final int len = data.length; |
| 93 | |
| 94 | if ((len & 0x01) != 0) { |
| 95 | throw new DecoderException("Odd number of characters."); |
| 96 | } |
nothing calls this directly
no outgoing calls
no test coverage detected