Skip to content

Commit 1b7b452

Browse files
committed
[CODEC-203] Add convenience method decodeHex(String).
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1701126 13f79535-47bb-0310-9956-ffa450edef68
1 parent cf1f05b commit 1b7b452

4 files changed

Lines changed: 43 additions & 10 deletions

File tree

src/changes/changes.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ The <action> type attribute can be add,update,fix,remove.
4949
<action dev="ggregory" type="add" issue="CODEC-195" due-to="Gary Gregory">Support SHA-224 in DigestUtils on Java 8</action>
5050
<action dev="ggregory" type="add" issue="CODEC-194" due-to="Gary Gregory">Support java.nio.ByteBuffer in org.apache.commons.codec.binary.Hex</action>
5151
<action dev="ggregory" type="add" issue="CODEC-193" due-to="Michael Donaghy">Support java.nio.ByteBuffer in DigestUtils</action>
52-
<action dev="ggregory" type="add" issue="CODEC-202" due-to="Oleg Kalnichevski">Add BaseNCodec.encode(byte[], int, int) input with offset and length parameters for Base64 and Base32.</action>
52+
<action dev="ggregory" type="add" issue="CODEC-202" due-to="Oleg Kalnichevski">Add BaseNCodec.encode(byte[], int, int) input with offset and length parameters for Base64 and Base32.</action>
53+
<action dev="ggregory" type="add" issue="CODEC-203" due-to="Gary Gregory">Add convenience method decodeHex(String).</action>
5354
</release>
5455
<release version="1.10" date="5 November 2014" description="Feature and fix release.">
5556
<action dev="ggregory" type="add" issue="CODEC-192" due-to="Thomas Neidhart">Add Daitch-Mokotoff Soundex</action>

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,22 @@ public class Hex implements BinaryEncoder, BinaryDecoder {
6464
private static final char[] DIGITS_UPPER =
6565
{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
6666

67+
/**
68+
* Converts a String representing hexadecimal values into an array of bytes of those same values. The
69+
* returned array will be half the length of the passed String, as it takes two characters to represent any given
70+
* byte. An exception is thrown if the passed String has an odd number of elements.
71+
*
72+
* @param data
73+
* A String containing hexadecimal digits
74+
* @return A byte array containing binary data decoded from the supplied char array.
75+
* @throws DecoderException
76+
* Thrown if an odd number or illegal of characters is supplied
77+
* @since 1.11
78+
*/
79+
public static byte[] decodeHex(String data) throws DecoderException {
80+
return decodeHex(data.toCharArray());
81+
}
82+
6783
/**
6884
* Converts an array of characters representing hexadecimal values into an array of bytes of those same values. The
6985
* returned array will be half the length of the passed array, as it takes two characters to represent any given

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,17 +1154,17 @@ public void testUUID() throws DecoderException {
11541154
final byte[][] ids = new byte[4][];
11551155

11561156
// ids[0] was chosen so that it encodes with at least one +.
1157-
ids[0] = Hex.decodeHex("94ed8d0319e4493399560fb67404d370".toCharArray());
1157+
ids[0] = Hex.decodeHex("94ed8d0319e4493399560fb67404d370");
11581158

11591159
// ids[1] was chosen so that it encodes with both / and +.
1160-
ids[1] = Hex.decodeHex("2bf7cc2701fe4397b49ebeed5acc7090".toCharArray());
1160+
ids[1] = Hex.decodeHex("2bf7cc2701fe4397b49ebeed5acc7090");
11611161

11621162
// ids[2] was chosen so that it encodes with at least one /.
1163-
ids[2] = Hex.decodeHex("64be154b6ffa40258d1a01288e7c31ca".toCharArray());
1163+
ids[2] = Hex.decodeHex("64be154b6ffa40258d1a01288e7c31ca");
11641164

11651165
// ids[3] was chosen so that it encodes with both / and +, with /
11661166
// right at the beginning.
1167-
ids[3] = Hex.decodeHex("ff7f8fc01cdb471a8c8b5a9306183fe8".toCharArray());
1167+
ids[3] = Hex.decodeHex("ff7f8fc01cdb471a8c8b5a9306183fe8");
11681168

11691169
final byte[][] standard = new byte[4][];
11701170
standard[0] = StringUtils.getBytesUtf8("lO2NAxnkSTOZVg+2dATTcA==");

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,6 @@ private boolean charsetSanityCheck(final String name) {
9696
}
9797
}
9898

99-
/**
100-
* @param data
101-
*/
10299
private void checkDecodeHexCharArrayOddCharacters(final char[] data) {
103100
try {
104101
Hex.decodeHex(data);
@@ -108,6 +105,15 @@ private void checkDecodeHexCharArrayOddCharacters(final char[] data) {
108105
}
109106
}
110107

108+
private void checkDecodeHexCharArrayOddCharacters(String data) {
109+
try {
110+
Hex.decodeHex(data);
111+
fail("An exception wasn't thrown when trying to decode an odd number of characters");
112+
} catch (final DecoderException e) {
113+
// Expected exception
114+
}
115+
}
116+
111117
private void log(final String s) {
112118
if (LOG) {
113119
System.out.println(s);
@@ -243,10 +249,15 @@ public void testDecodeByteBufferOddCharacters() {
243249
}
244250

245251
@Test
246-
public void testDecodeCharArrayEmpty() throws DecoderException {
252+
public void testDecodeHexCharArrayEmpty() throws DecoderException {
247253
assertTrue(Arrays.equals(new byte[0], Hex.decodeHex(new char[0])));
248254
}
249255

256+
@Test
257+
public void testDecodeHexStringEmpty() throws DecoderException {
258+
assertTrue(Arrays.equals(new byte[0], Hex.decodeHex("")));
259+
}
260+
250261
@Test
251262
public void testDecodeClassCastException() {
252263
try {
@@ -262,6 +273,11 @@ public void testDecodeHexCharArrayOddCharacters1() {
262273
checkDecodeHexCharArrayOddCharacters(new char[] { 'A' });
263274
}
264275

276+
@Test
277+
public void testDecodeHexStringOddCharacters1() {
278+
checkDecodeHexCharArrayOddCharacters("A");
279+
}
280+
265281
@Test
266282
public void testDecodeHexCharArrayOddCharacters3() {
267283
checkDecodeHexCharArrayOddCharacters(new char[] { 'A', 'B', 'C' });
@@ -318,7 +334,7 @@ public void testEncodeClassCastException() {
318334
}
319335

320336
@Test
321-
public void testEncodeDecodeRandom() throws DecoderException, EncoderException {
337+
public void testEncodeDecodeHexCharArrayRandom() throws DecoderException, EncoderException {
322338
final Random random = new Random();
323339

324340
final Hex hex = new Hex();

0 commit comments

Comments
 (0)