1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.codec.binary;
  17. import org.apache.commons.codec.BinaryDecoder;
  18. import org.apache.commons.codec.BinaryEncoder;
  19. import org.apache.commons.codec.DecoderException;
  20. import org.apache.commons.codec.EncoderException;
  21. /**
  22. * Hex encoder and decoder.
  23. *
  24. * @since 1.1
  25. * @author Apache Software Foundation
  26. * @version $Id: Hex.java,v 1.13 2004/04/18 18:22:33 ggregory Exp $
  27. */
  28. public class Hex implements BinaryEncoder, BinaryDecoder {
  29. /**
  30. * Used building output as Hex
  31. */
  32. private static final char[] DIGITS = {
  33. '0', '1', '2', '3', '4', '5', '6', '7',
  34. '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
  35. };
  36. /**
  37. * Converts an array of characters representing hexidecimal values into an
  38. * array of bytes of those same values. The returned array will be half the
  39. * length of the passed array, as it takes two characters to represent any
  40. * given byte. An exception is thrown if the passed char array has an odd
  41. * number of elements.
  42. *
  43. * @param data An array of characters containing hexidecimal digits
  44. * @return A byte array containing binary data decoded from
  45. * the supplied char array.
  46. * @throws DecoderException Thrown if an odd number or illegal of characters
  47. * is supplied
  48. */
  49. public static byte[] decodeHex(char[] data) throws DecoderException {
  50. int len = data.length;
  51. if ((len & 0x01) != 0) {
  52. throw new DecoderException("Odd number of characters.");
  53. }
  54. byte[] out = new byte[len >> 1];
  55. // two characters form the hex value.
  56. for (int i = 0, j = 0; j < len; i++) {
  57. int f = toDigit(data[j], j) << 4;
  58. j++;
  59. f = f | toDigit(data[j], j);
  60. j++;
  61. out[i] = (byte) (f & 0xFF);
  62. }
  63. return out;
  64. }
  65. /**
  66. * Converts a hexadecimal character to an integer.
  67. *
  68. * @param ch A character to convert to an integer digit
  69. * @param index The index of the character in the source
  70. * @return An integer
  71. * @throws DecoderException Thrown if ch is an illegal hex character
  72. */
  73. protected static int toDigit(char ch, int index) throws DecoderException {
  74. int digit = Character.digit(ch, 16);
  75. if (digit == -1) {
  76. throw new DecoderException("Illegal hexadecimal charcter " + ch + " at index " + index);
  77. }
  78. return digit;
  79. }
  80. /**
  81. * Converts an array of bytes into an array of characters representing the hexidecimal values of each byte in order.
  82. * The returned array will be double the length of the passed array, as it takes two characters to represent any
  83. * given byte.
  84. *
  85. * @param data
  86. * a byte[] to convert to Hex characters
  87. * @return A char[] containing hexidecimal characters
  88. */
  89. public static char[] encodeHex(byte[] data) {
  90. int l = data.length;
  91. char[] out = new char[l << 1];
  92. // two characters form the hex value.
  93. for (int i = 0, j = 0; i < l; i++) {
  94. out[j++] = DIGITS[(0xF0 & data[i]) >>> 4 ];
  95. out[j++] = DIGITS[ 0x0F & data[i] ];
  96. }
  97. return out;
  98. }
  99. /**
  100. * Converts an array of character bytes representing hexidecimal values into an
  101. * array of bytes of those same values. The returned array will be half the
  102. * length of the passed array, as it takes two characters to represent any
  103. * given byte. An exception is thrown if the passed char array has an odd
  104. * number of elements.
  105. *
  106. * @param array An array of character bytes containing hexidecimal digits
  107. * @return A byte array containing binary data decoded from
  108. * the supplied byte array (representing characters).
  109. * @throws DecoderException Thrown if an odd number of characters is supplied
  110. * to this function
  111. * @see #decodeHex(char[])
  112. */
  113. public byte[] decode(byte[] array) throws DecoderException {
  114. return decodeHex(new String(array).toCharArray());
  115. }
  116. /**
  117. * Converts a String or an array of character bytes representing hexidecimal values into an
  118. * array of bytes of those same values. The returned array will be half the
  119. * length of the passed String or array, as it takes two characters to represent any
  120. * given byte. An exception is thrown if the passed char array has an odd
  121. * number of elements.
  122. *
  123. * @param object A String or, an array of character bytes containing hexidecimal digits
  124. * @return A byte array containing binary data decoded from
  125. * the supplied byte array (representing characters).
  126. * @throws DecoderException Thrown if an odd number of characters is supplied
  127. * to this function or the object is not a String or char[]
  128. * @see #decodeHex(char[])
  129. */
  130. public Object decode(Object object) throws DecoderException {
  131. try {
  132. char[] charArray = object instanceof String ? ((String) object).toCharArray() : (char[]) object;
  133. return decodeHex(charArray);
  134. } catch (ClassCastException e) {
  135. throw new DecoderException(e.getMessage());
  136. }
  137. }
  138. /**
  139. * Converts an array of bytes into an array of bytes for the characters representing the
  140. * hexidecimal values of each byte in order. The returned array will be
  141. * double the length of the passed array, as it takes two characters to
  142. * represent any given byte.
  143. *
  144. * @param array a byte[] to convert to Hex characters
  145. * @return A byte[] containing the bytes of the hexidecimal characters
  146. * @see #encodeHex(byte[])
  147. */
  148. public byte[] encode(byte[] array) {
  149. return new String(encodeHex(array)).getBytes();
  150. }
  151. /**
  152. * Converts a String or an array of bytes into an array of characters representing the
  153. * hexidecimal values of each byte in order. The returned array will be
  154. * double the length of the passed String or array, as it takes two characters to
  155. * represent any given byte.
  156. *
  157. * @param object a String, or byte[] to convert to Hex characters
  158. * @return A char[] containing hexidecimal characters
  159. * @throws EncoderException Thrown if the given object is not a String or byte[]
  160. * @see #encodeHex(byte[])
  161. */
  162. public Object encode(Object object) throws EncoderException {
  163. try {
  164. byte[] byteArray = object instanceof String ? ((String) object).getBytes() : (byte[]) object;
  165. return encodeHex(byteArray);
  166. } catch (ClassCastException e) {
  167. throw new EncoderException(e.getMessage());
  168. }
  169. }
  170. }