[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
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 6e01139..b7a3d8b 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -66,6 +66,7 @@
<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>
<action issue="CODEC-203" dev="ggregory" type="add" due-to="Gary Gregory">Add convenience method decodeHex(String).</action>
<action issue="CODEC-205" dev="ggregory" type="add" due-to="Gary Gregory">Add faster CRC32 implementation.</action>
+ <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>
</release>
<release version="1.10" date="5 November 2014" description="Feature and fix release.">
<action dev="ggregory" type="add" issue="CODEC-192" due-to="Thomas Neidhart">Add Daitch-Mokotoff Soundex</action>
diff --git a/src/main/java/org/apache/commons/codec/binary/Hex.java b/src/main/java/org/apache/commons/codec/binary/Hex.java
index cccd358..421b413 100644
--- a/src/main/java/org/apache/commons/codec/binary/Hex.java
+++ b/src/main/java/org/apache/commons/codec/binary/Hex.java
@@ -227,6 +227,21 @@
}
/**
+ * Converts an array of bytes into a String representing the hexadecimal values of each byte in order. The returned
+ * String will be double the length of the passed array, as it takes two characters to represent any given byte.
+ *
+ * @param data
+ * a byte[] to convert to Hex characters
+ * @param toLowerCase
+ * <code>true</code> converts to lowercase, <code>false</code> to uppercase
+ * @return A String containing lower-case hexadecimal characters
+ * @since 1.11
+ */
+ public static String encodeHexString(final byte[] data, boolean toLowerCase) {
+ return new String(encodeHex(data, toLowerCase));
+ }
+
+ /**
* Converts a byte buffer into a String representing the hexadecimal values of each byte in order. The returned
* String will be double the length of the passed array, as it takes two characters to represent any given byte.
*
@@ -240,6 +255,21 @@
}
/**
+ * Converts a byte buffer into a String representing the hexadecimal values of each byte in order. The returned
+ * String will be double the length of the passed array, as it takes two characters to represent any given byte.
+ *
+ * @param data
+ * a byte buffer to convert to Hex characters
+ * @param toLowerCase
+ * <code>true</code> converts to lowercase, <code>false</code> to uppercase
+ * @return A String containing lower-case hexadecimal characters
+ * @since 1.11
+ */
+ public static String encodeHexString(final ByteBuffer data, boolean toLowerCase) {
+ return new String(encodeHex(data, toLowerCase));
+ }
+
+ /**
* Converts a hexadecimal character to an integer.
*
* @param ch
diff --git a/src/test/java/org/apache/commons/codec/binary/HexTest.java b/src/test/java/org/apache/commons/codec/binary/HexTest.java
index 3dc37c7..0b8f431 100644
--- a/src/test/java/org/apache/commons/codec/binary/HexTest.java
+++ b/src/test/java/org/apache/commons/codec/binary/HexTest.java
@@ -455,6 +455,26 @@
}
@Test
+ public void testEncodeHexByteString_ByteArrayBoolean_ToLowerCase() {
+ assertEquals("0a", Hex.encodeHexString(new byte[] { 10 }, true));
+ }
+
+ @Test
+ public void testEncodeHexByteString_ByteArrayBoolean_ToUpperCase() {
+ assertEquals("0A", Hex.encodeHexString(new byte[] { 10 }, false));
+ }
+
+ @Test
+ public void testEncodeHexByteString_ByteBufferBoolean_ToLowerCase() {
+ assertEquals("0a", Hex.encodeHexString(ByteBuffer.wrap(new byte[] { 10 }), true));
+ }
+
+ @Test
+ public void testEncodeHexByteString_ByteBufferBoolean_ToUpperCase() {
+ assertEquals("0A", Hex.encodeHexString(ByteBuffer.wrap(new byte[] { 10 }), false));
+ }
+
+ @Test
public void testEncodeStringEmpty() throws EncoderException {
assertTrue(Arrays.equals(new char[0], (char[]) new Hex().encode("")));
}