Author: ggregory
Date: Tue Jan 20 12:53:04 2009
New Revision: 736102

URL: http://svn.apache.org/viewvc?rev=736102&view=rev
Log:
[CODEC-74] Allow for uppercase letters as the output of Hex.encodeHex()

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

Modified: 
commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Hex.java
URL: 
http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Hex.java?rev=736102&r1=736101&r2=736102&view=diff
==============================================================================
--- 
commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Hex.java 
(original)
+++ 
commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Hex.java 
Tue Jan 20 12:53:04 2009
@@ -34,11 +34,19 @@
     /** 
      * Used to build output as Hex 
      */
-    private static final char[] DIGITS = {
+    private static final char[] DIGITS_LOWER = {
         '0', '1', '2', '3', '4', '5', '6', '7',
            '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
     };
 
+    /** 
+     * Used to build output as Hex 
+     */
+    private static final char[] DIGITS_UPPER = {
+        '0', '1', '2', '3', '4', '5', '6', '7',
+           '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
+    };
+
     /**
      * Converts an array of characters representing hexadecimal values into an
      * array of bytes of those same values. The returned array will be half the
@@ -96,22 +104,52 @@
      * given byte.
      * 
      * @param data
-     *                  a byte[] to convert to Hex characters
+     *            a byte[] to convert to Hex characters
      * @return A char[] containing hexadecimal characters
      */
     public static char[] encodeHex(byte[] data) {
+        return encodeHex(data, true);
+    }
+
+    /**
+     * Converts an array of bytes into an array of characters representing the 
hexadecimal values of each byte in order.
+     * The returned array 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 char[] containing hexadecimal characters
+     */
+    public static char[] encodeHex(byte[] data, boolean toLowerCase) {
+        return encodeHex(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
+    }
+
+    /**
+     * Converts an array of bytes into an array of characters representing the 
hexadecimal values of each byte in order.
+     * The returned array 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 toDigits
+     *            the output alphabet
+     * @return A char[] containing hexadecimal characters
+     */
+    protected static char[] encodeHex(byte[] data, char[] toDigits) {
 
         int l = data.length;
 
-           char[] out = new char[l << 1];
+        char[] out = new char[l << 1];
 
-           // two characters form the hex value.
-           for (int i = 0, j = 0; i < l; i++) {
-               out[j++] = DIGITS[(0xF0 & data[i]) >>> 4 ];
-               out[j++] = DIGITS[ 0x0F & data[i] ];
-           }
+        // two characters form the hex value.
+        for (int i = 0, j = 0; i < l; i++) {
+            out[j++] = toDigits[(0xF0 & data[i]) >>> 4];
+            out[j++] = toDigits[0x0F & data[i]];
+        }
 
-           return out;
+        return out;
     }
 
     /**

Modified: 
commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/HexTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/HexTest.java?rev=736102&r1=736101&r2=736102&view=diff
==============================================================================
--- 
commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/HexTest.java
 (original)
+++ 
commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/HexTest.java
 Tue Jan 20 12:53:04 2009
@@ -160,9 +160,27 @@
             new String(c));
     }
 
-    public void testHelloWorld() {
+    public void testHelloWorldLowerCaseHex() {
         byte[] b = "Hello World".getBytes();
-        char[] c = Hex.encodeHex(b);
-        assertEquals("48656c6c6f20576f726c64", new String(c));
+        final String expected = "48656c6c6f20576f726c64";
+        char[] actual;
+        actual = Hex.encodeHex(b);
+        assertTrue(expected.equals(new String(actual)));
+        actual = Hex.encodeHex(b, true);
+        assertTrue(expected.equals(new String(actual)));
+        actual = Hex.encodeHex(b, false);
+        assertFalse(expected.equals(new String(actual)));
+    }
+
+    public void testHelloWorldUpperCaseHex() {
+        byte[] b = "Hello World".getBytes();
+        final String expected = "48656C6C6F20576F726C64";
+        char[] actual;
+        actual = Hex.encodeHex(b);
+        assertFalse(expected.equals(new String(actual)));
+        actual = Hex.encodeHex(b, true);
+        assertFalse(expected.equals(new String(actual)));
+        actual = Hex.encodeHex(b, false);
+        assertTrue(expected.equals(new String(actual)));
     }
 }


Reply via email to