Skip to content

Commit 981c000

Browse files
committed
[CODEC-74] Allow for uppercase letters as the output of Hex.encodeHex()
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@736102 13f79535-47bb-0310-9956-ffa450edef68
1 parent 94a4db4 commit 981c000

2 files changed

Lines changed: 68 additions & 12 deletions

File tree

src/java/org/apache/commons/codec/binary/Hex.java

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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
/**

src/test/org/apache/commons/codec/binary/HexTest.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,27 @@ public void testEncodeZeroes() {
160160
new String(c));
161161
}
162162

163-
public void testHelloWorld() {
163+
public void testHelloWorldLowerCaseHex() {
164164
byte[] b = "Hello World".getBytes();
165-
char[] c = Hex.encodeHex(b);
166-
assertEquals("48656c6c6f20576f726c64", new String(c));
165+
final String expected = "48656c6c6f20576f726c64";
166+
char[] actual;
167+
actual = Hex.encodeHex(b);
168+
assertTrue(expected.equals(new String(actual)));
169+
actual = Hex.encodeHex(b, true);
170+
assertTrue(expected.equals(new String(actual)));
171+
actual = Hex.encodeHex(b, false);
172+
assertFalse(expected.equals(new String(actual)));
173+
}
174+
175+
public void testHelloWorldUpperCaseHex() {
176+
byte[] b = "Hello World".getBytes();
177+
final String expected = "48656C6C6F20576F726C64";
178+
char[] actual;
179+
actual = Hex.encodeHex(b);
180+
assertFalse(expected.equals(new String(actual)));
181+
actual = Hex.encodeHex(b, true);
182+
assertFalse(expected.equals(new String(actual)));
183+
actual = Hex.encodeHex(b, false);
184+
assertTrue(expected.equals(new String(actual)));
167185
}
168186
}

0 commit comments

Comments
 (0)