You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by to...@apache.org on 2003/02/25 05:16:14 UTC

cvs commit: jakarta-commons-sandbox/codec/src/java/org/apache/commons/codec/binary Hex.java

tobrien     2003/02/24 20:16:14

  Modified:    codec    TODO
  Added:       codec/src/test/org/apache/commons/codec/binary TestHex.java
               codec/src/java/org/apache/commons/codec/binary Hex.java
  Log:
  Added an initial version of Hex
  
  Revision  Changes    Path
  1.10      +1 -2      jakarta-commons-sandbox/codec/TODO
  
  Index: TODO
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/codec/TODO,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- TODO	24 Feb 2003 10:48:45 -0000	1.9
  +++ TODO	25 Feb 2003 04:16:14 -0000	1.10
  @@ -21,8 +21,6 @@
   
   * Add a uu encode and decode to binary package
   
  -* Add a Hex implementation to binary package
  -
   * Add a Rot13 implementation to text package 
   
   
  @@ -49,6 +47,7 @@
   
   ** DONE (this list will help us generate RELEASE-NOTES when it comes time for a release)
   
  +2/24/03 - TOB - * Added HEx implementation from Christopher O'Brien
   2/1?/03 - TOB - * Refactor Base64 to implement both Encoder and Decoder 
   2/1?/03 - TOB - * Add a Decoder interface 
   2/7/03 - TOB - Removed all deprecated classes this includes
  
  
  
  1.1                  jakarta-commons-sandbox/codec/src/test/org/apache/commons/codec/binary/TestHex.java
  
  Index: TestHex.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/codec/src/test/org/apache/commons/codec/binary/TestHex.java,v 1.1 2003/02/25 04:16:14 tobrien Exp $
   * $Revision: 1.1 $
   * $Date: 2003/02/25 04:16:14 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001-2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  package org.apache.commons.codec.binary;
  
  import java.util.Arrays;
  import java.util.Random;
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  import junit.textui.TestRunner;
  
  /**
   * 
   * @author <a href="mailto:siege@preoccupied.net">Christopher O'Brien</a>
   * @author <a href="mailto:tobrien@apache.org">Tim O'Brien</a>
   */
  
  public class TestHex extends TestCase {
  
      public TestHex(String name) {
          super(name);
      }
  
      public static Test suite() {
          TestSuite suite = new TestSuite();
  
          suite.addTest(new TestHex("testEncodeEmpty"));
          suite.addTest(new TestHex("testEncodeZeroes"));
          suite.addTest(new TestHex("testHelloWorld"));
          suite.addTest(new TestHex("testEncodeDecodeRandom"));
  
          return suite;
      }
  
      public static final void main(String[] args) throws Exception {
          TestRunner runner = new TestRunner();
          runner.doRun(suite());
      }
  
      public void testEncodeEmpty() throws Exception {
          char[] c = Hex.hex(new byte[0]);
          assertTrue(Arrays.equals(new char[0], c));
      }
  
      public void testEncodeZeroes() throws Exception {
          char[] c = Hex.hex(new byte[36]);
          assertEquals(
              "000000000000000000000000000000000000"
                  + "000000000000000000000000000000000000",
              new String(c));
      }
  
      public void testHelloWorld() throws Exception {
          byte[] b = "Hello World".getBytes();
          char[] c = Hex.hex(b);
          assertEquals("48656c6c6f20576f726c64", new String(c));
      }
  
      public void testEncodeDecodeRandom() throws Exception {
          Random random = new Random();
  
          for (int i = 5; i > 0; i--) {
              byte[] data = new byte[random.nextInt(10000) + 1];
              random.nextBytes(data);
  
              char[] enc = Hex.hex(data);
              byte[] data2 = Hex.hexdecode(enc);
  
              assertTrue(Arrays.equals(data, data2));
          }
      }
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/codec/src/java/org/apache/commons/codec/binary/Hex.java
  
  Index: Hex.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Commons" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Commons", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package org.apache.commons.codec.binary;
  
  /**
   * Hex encoder/decoder
   * 
   * @author <a href="mailto:siege@preoccupied.net">Christopher O'Brien</a>
   * @author <a href="mailto:tobrien@apache.org">Tim O'Brien</a>
   */
  public class Hex {
  
  
      /** for building output as Hex */
      private static final char[] digits = {
  	   '0', '1', '2', '3', '4', '5', '6', '7',
  	   '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
      };
  
  
  
      /**
         Converts an array of bytes into an array of characters representing the
         hexidecimal 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.
      */
      public static char[] hex(byte[] data) {
  
  	   int l = data.length;
  
  	   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] ];
  	   }
  
  	   return out;
      }
  
  
  
      /**
         Converts an array of characters representing hexidecimal values into an
         array of bytes of those same values. The returned array will be half the
         length of the passed array, as it takes two characters to represent any
         given byte. An exception is thrown if the passed char array has an odd
         number of elements.
      */
      public static byte[] hexdecode(char[] data) throws Exception {
  	
  	   int l = data.length;
  
  	   if((l & 0x01) != 0) {
  	       throw new Exception("odd number of characters.");
  	   }
  
  	   byte[] out = new byte[l >> 1];
  	
  	   // two characters form the hex value.
  	   for(int i = 0, j = 0; j < l; i++) {
  	       int f = Character.digit(data[j++], 16) << 4;
  	       f = f | Character.digit(data[j++], 16);
  	       out[i] = (byte) (f & 0xFF);
  	   }
  
  	   return out;
      }
  
  }
  
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org