@@ -34,11 +34,19 @@ public class Hex implements BinaryEncoder, BinaryDecoder {
3434 /**
3535 * Used to build output as Hex
3636 */
37- private static final char [] DIGITS = {
37+ private static final char [] DIGITS_LOWER = {
3838 '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' ,
3939 '8' , '9' , 'a' , 'b' , 'c' , 'd' , 'e' , 'f'
4040 };
4141
42+ /**
43+ * Used to build output as Hex
44+ */
45+ private static final char [] DIGITS_UPPER = {
46+ '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' ,
47+ '8' , '9' , 'A' , 'B' , 'C' , 'D' , 'E' , 'F'
48+ };
49+
4250 /**
4351 * Converts an array of characters representing hexadecimal values into an
4452 * array of bytes of those same values. The returned array will be half the
@@ -96,22 +104,52 @@ protected static int toDigit(char ch, int index) throws DecoderException {
96104 * given byte.
97105 *
98106 * @param data
99- * a byte[] to convert to Hex characters
107+ * a byte[] to convert to Hex characters
100108 * @return A char[] containing hexadecimal characters
101109 */
102110 public static char [] encodeHex (byte [] data ) {
111+ return encodeHex (data , true );
112+ }
113+
114+ /**
115+ * Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order.
116+ * The returned array will be double the length of the passed array, as it takes two characters to represent any
117+ * given byte.
118+ *
119+ * @param data
120+ * a byte[] to convert to Hex characters
121+ * @param toLowerCase
122+ * <code>true</code> converts to lowercase, <code>false</code> to uppercase
123+ * @return A char[] containing hexadecimal characters
124+ */
125+ public static char [] encodeHex (byte [] data , boolean toLowerCase ) {
126+ return encodeHex (data , toLowerCase ? DIGITS_LOWER : DIGITS_UPPER );
127+ }
128+
129+ /**
130+ * Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order.
131+ * The returned array will be double the length of the passed array, as it takes two characters to represent any
132+ * given byte.
133+ *
134+ * @param data
135+ * a byte[] to convert to Hex characters
136+ * @param toDigits
137+ * the output alphabet
138+ * @return A char[] containing hexadecimal characters
139+ */
140+ protected static char [] encodeHex (byte [] data , char [] toDigits ) {
103141
104142 int l = data .length ;
105143
106- char [] out = new char [l << 1 ];
144+ char [] out = new char [l << 1 ];
107145
108- // two characters form the hex value.
109- for (int i = 0 , j = 0 ; i < l ; i ++) {
110- out [j ++] = DIGITS [(0xF0 & data [i ]) >>> 4 ];
111- out [j ++] = DIGITS [ 0x0F & data [i ] ];
112- }
146+ // two characters form the hex value.
147+ for (int i = 0 , j = 0 ; i < l ; i ++) {
148+ out [j ++] = toDigits [(0xF0 & data [i ]) >>> 4 ];
149+ out [j ++] = toDigits [ 0x0F & data [i ]];
150+ }
113151
114- return out ;
152+ return out ;
115153 }
116154
117155 /**
0 commit comments