Skip to content

Commit f0028e1

Browse files
committed
[CODEC-224] Add convenience API org.apache.commons.codec.binary.Hex.encodeHexString(byte[]|ByteBuffer, boolean).
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1754055 13f79535-47bb-0310-9956-ffa450edef68
1 parent cc27943 commit f0028e1

3 files changed

Lines changed: 51 additions & 0 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ The <action> type attribute can be add,update,fix,remove.
6666
<action issue="CODEC-202" dev="ggregory" type="add" due-to="Oleg Kalnichevski">Add BaseNCodec.encode(byte[], int, int) input with offset and length parameters for Base64 and Base32.</action>
6767
<action issue="CODEC-203" dev="ggregory" type="add" due-to="Gary Gregory">Add convenience method decodeHex(String).</action>
6868
<action issue="CODEC-205" dev="ggregory" type="add" due-to="Gary Gregory">Add faster CRC32 implementation.</action>
69+
<action issue="CODEC-224" dev="ggregory" type="add" due-to="Gary Gregory">Add convenience API org.apache.commons.codec.binary.Hex.encodeHexString(byte[]|ByteBuffer, boolean).</action>
6970
</release>
7071
<release version="1.10" date="5 November 2014" description="Feature and fix release.">
7172
<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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,21 @@ public static String encodeHexString(final byte[] data) {
226226
return new String(encodeHex(data));
227227
}
228228

229+
/**
230+
* Converts an array of bytes into a String representing the hexadecimal values of each byte in order. The returned
231+
* String will be double the length of the passed array, as it takes two characters to represent any given byte.
232+
*
233+
* @param data
234+
* a byte[] to convert to Hex characters
235+
* @param toLowerCase
236+
* <code>true</code> converts to lowercase, <code>false</code> to uppercase
237+
* @return A String containing lower-case hexadecimal characters
238+
* @since 1.11
239+
*/
240+
public static String encodeHexString(final byte[] data, boolean toLowerCase) {
241+
return new String(encodeHex(data, toLowerCase));
242+
}
243+
229244
/**
230245
* Converts a byte buffer into a String representing the hexadecimal values of each byte in order. The returned
231246
* String will be double the length of the passed array, as it takes two characters to represent any given byte.
@@ -239,6 +254,21 @@ public static String encodeHexString(final ByteBuffer data) {
239254
return new String(encodeHex(data));
240255
}
241256

257+
/**
258+
* Converts a byte buffer into a String representing the hexadecimal values of each byte in order. The returned
259+
* String will be double the length of the passed array, as it takes two characters to represent any given byte.
260+
*
261+
* @param data
262+
* a byte buffer to convert to Hex characters
263+
* @param toLowerCase
264+
* <code>true</code> converts to lowercase, <code>false</code> to uppercase
265+
* @return A String containing lower-case hexadecimal characters
266+
* @since 1.11
267+
*/
268+
public static String encodeHexString(final ByteBuffer data, boolean toLowerCase) {
269+
return new String(encodeHex(data, toLowerCase));
270+
}
271+
242272
/**
243273
* Converts a hexadecimal character to an integer.
244274
*

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,26 @@ public void testEncodeHexByteString_ByteArrayOfZeroes() {
454454
assertEquals("000000000000000000000000000000000000000000000000000000000000000000000000", c);
455455
}
456456

457+
@Test
458+
public void testEncodeHexByteString_ByteArrayBoolean_ToLowerCase() {
459+
assertEquals("0a", Hex.encodeHexString(new byte[] { 10 }, true));
460+
}
461+
462+
@Test
463+
public void testEncodeHexByteString_ByteArrayBoolean_ToUpperCase() {
464+
assertEquals("0A", Hex.encodeHexString(new byte[] { 10 }, false));
465+
}
466+
467+
@Test
468+
public void testEncodeHexByteString_ByteBufferBoolean_ToLowerCase() {
469+
assertEquals("0a", Hex.encodeHexString(ByteBuffer.wrap(new byte[] { 10 }), true));
470+
}
471+
472+
@Test
473+
public void testEncodeHexByteString_ByteBufferBoolean_ToUpperCase() {
474+
assertEquals("0A", Hex.encodeHexString(ByteBuffer.wrap(new byte[] { 10 }), false));
475+
}
476+
457477
@Test
458478
public void testEncodeStringEmpty() throws EncoderException {
459479
assertTrue(Arrays.equals(new char[0], (char[]) new Hex().encode("")));

0 commit comments

Comments
 (0)