1 55 package org.apache.soap.encoding ; 56 57 import java.io.ByteArrayOutputStream ; 58 59 64 public class Hex extends Object { 65 66 byte[] m_value = null; 67 68 public Hex() { 69 } 70 71 public Hex(String string){ 72 m_value = decode(string); 73 } 74 75 public byte[] getBytes(){ 76 return m_value; 77 } 78 79 public String toString(){ 80 return encode(m_value); 81 } 82 83 public int hashCode(){ 84 return super.hashCode(); 86 } 87 88 public boolean equals(java.lang.Object object){ 89 String s1 = object.toString(); 91 String s2 = this.toString(); 92 return s1.equals(s2); 93 } 94 95 public static final String ERROR_ODD_NUMBER_OF_DIGITS = 96 "Odd number of digits in hex string"; 97 public static final String ERROR_BAD_CHARACTER_IN_HEX_STRING = 98 "Bad character or insufficient number of characters in hex string"; 99 100 102 public static final int[] DEC = { 104 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 105 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 106 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 107 00, 01, 02, 03, 04, 05, 06, 07, 8, 9, -1, -1, -1, -1, -1, -1, 108 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, 109 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 110 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, 111 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 112 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 113 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 114 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 115 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 116 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 117 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 118 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 119 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 120 }; 121 122 132 public static byte[] decode(String digits) { 133 134 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 135 for (int i = 0; i < digits.length(); i += 2) { 136 char c1 = digits.charAt(i); 137 if ((i+1) >= digits.length()) 138 throw new IllegalArgumentException 139 (ERROR_ODD_NUMBER_OF_DIGITS); 140 char c2 = digits.charAt(i + 1); 141 byte b = 0; 142 if ((c1 >= '0') && (c1 <= '9')) 143 b += ((c1 - '0') * 16); 144 else if ((c1 >= 'a') && (c1 <= 'f')) 145 b += ((c1 - 'a' + 10) * 16); 146 else if ((c1 >= 'A') && (c1 <= 'F')) 147 b += ((c1 - 'A' + 10) * 16); 148 else 149 throw new IllegalArgumentException 150 (ERROR_BAD_CHARACTER_IN_HEX_STRING); 151 if ((c2 >= '0') && (c2 <= '9')) 152 b += (c2 - '0'); 153 else if ((c2 >= 'a') && (c2 <= 'f')) 154 b += (c2 - 'a' + 10); 155 else if ((c2 >= 'A') && (c2 <= 'F')) 156 b += (c2 - 'A' + 10); 157 else 158 throw new IllegalArgumentException 159 (ERROR_BAD_CHARACTER_IN_HEX_STRING); 160 baos.write(b); 161 } 162 return (baos.toByteArray()); 163 164 } 165 166 167 173 public static String encode(byte bytes[]) { 174 175 StringBuffer sb = new StringBuffer (bytes.length * 2); 176 for (int i = 0; i < bytes.length; i++) { 177 sb.append(convertDigit((int) (bytes[i] >> 4))); 178 sb.append(convertDigit((int) (bytes[i] & 0x0f))); 179 } 180 return (sb.toString()); 181 182 } 183 184 193 public static int convert2Int( byte[] hex ) { 194 196 int len; 199 if(hex.length < 4 ) return 0; 200 if( DEC[hex[0]]<0 ) 201 throw new IllegalArgumentException (ERROR_BAD_CHARACTER_IN_HEX_STRING); 202 len = DEC[hex[0]]; 203 len = len << 4; 204 if( DEC[hex[1]]<0 ) 205 throw new IllegalArgumentException (ERROR_BAD_CHARACTER_IN_HEX_STRING); 206 len += DEC[hex[1]]; 207 len = len << 4; 208 if( DEC[hex[2]]<0 ) 209 throw new IllegalArgumentException (ERROR_BAD_CHARACTER_IN_HEX_STRING); 210 len += DEC[hex[2]]; 211 len = len << 4; 212 if( DEC[hex[3]]<0 ) 213 throw new IllegalArgumentException (ERROR_BAD_CHARACTER_IN_HEX_STRING); 214 len += DEC[hex[3]]; 215 return len; 216 } 217 218 224 private static char convertDigit(int value) { 225 226 value &= 0x0f; 227 if (value >= 10) 228 return ((char) (value - 10 + 'a')); 229 else 230 return ((char) (value + '0')); 231 232 } 233 } 234 | Popular Tags |