You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2016/07/25 21:50:40 UTC

svn commit: r1754055 - in /commons/proper/codec/trunk/src: changes/changes.xml main/java/org/apache/commons/codec/binary/Hex.java test/java/org/apache/commons/codec/binary/HexTest.java

Author: ggregory
Date: Mon Jul 25 21:50:39 2016
New Revision: 1754055

URL: http://svn.apache.org/viewvc?rev=1754055&view=rev
Log:
[CODEC-224] Add convenience API org.apache.commons.codec.binary.Hex.encodeHexString(byte[]|ByteBuffer, boolean).

Modified:
    commons/proper/codec/trunk/src/changes/changes.xml
    commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/binary/Hex.java
    commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/HexTest.java

Modified: commons/proper/codec/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/changes/changes.xml?rev=1754055&r1=1754054&r2=1754055&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/changes/changes.xml (original)
+++ commons/proper/codec/trunk/src/changes/changes.xml Mon Jul 25 21:50:39 2016
@@ -66,6 +66,7 @@ The <action> type attribute can be add,u
       <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>

Modified: commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/binary/Hex.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/binary/Hex.java?rev=1754055&r1=1754054&r2=1754055&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/binary/Hex.java (original)
+++ commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/binary/Hex.java Mon Jul 25 21:50:39 2016
@@ -227,6 +227,21 @@ public class Hex implements BinaryEncode
     }
 
     /**
+     * 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 @@ public class Hex implements BinaryEncode
     }
 
     /**
+     * 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

Modified: commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/HexTest.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/HexTest.java?rev=1754055&r1=1754054&r2=1754055&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/HexTest.java (original)
+++ commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/HexTest.java Mon Jul 25 21:50:39 2016
@@ -455,6 +455,26 @@ public class HexTest {
     }
 
     @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("")));
     }