1 17 18 19 package org.apache.catalina.util; 20 21 import java.io.ByteArrayOutputStream ; 22 23 29 30 public final class HexUtils { 31 33 public static final int[] DEC = { 35 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 36 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 37 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 38 00, 01, 02, 03, 04, 05, 06, 07, 8, 9, -1, -1, -1, -1, -1, -1, 39 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, 40 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 41 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, 42 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 43 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 44 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 45 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 46 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 47 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 48 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 49 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 50 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 51 }; 52 53 54 55 58 private static StringManager sm = 59 StringManager.getManager("org.apache.catalina.util"); 60 61 62 72 public static byte[] convert(String digits) { 73 74 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 75 for (int i = 0; i < digits.length(); i += 2) { 76 char c1 = digits.charAt(i); 77 if ((i+1) >= digits.length()) 78 throw new IllegalArgumentException 79 (sm.getString("hexUtil.odd")); 80 char c2 = digits.charAt(i + 1); 81 byte b = 0; 82 if ((c1 >= '0') && (c1 <= '9')) 83 b += ((c1 - '0') * 16); 84 else if ((c1 >= 'a') && (c1 <= 'f')) 85 b += ((c1 - 'a' + 10) * 16); 86 else if ((c1 >= 'A') && (c1 <= 'F')) 87 b += ((c1 - 'A' + 10) * 16); 88 else 89 throw new IllegalArgumentException 90 (sm.getString("hexUtil.bad")); 91 if ((c2 >= '0') && (c2 <= '9')) 92 b += (c2 - '0'); 93 else if ((c2 >= 'a') && (c2 <= 'f')) 94 b += (c2 - 'a' + 10); 95 else if ((c2 >= 'A') && (c2 <= 'F')) 96 b += (c2 - 'A' + 10); 97 else 98 throw new IllegalArgumentException 99 (sm.getString("hexUtil.bad")); 100 baos.write(b); 101 } 102 return (baos.toByteArray()); 103 104 } 105 106 107 113 public static String convert(byte bytes[]) { 114 115 StringBuffer sb = new StringBuffer (bytes.length * 2); 116 for (int i = 0; i < bytes.length; i++) { 117 sb.append(convertDigit((int) (bytes[i] >> 4))); 118 sb.append(convertDigit((int) (bytes[i] & 0x0f))); 119 } 120 return (sb.toString()); 121 122 } 123 124 133 public static int convert2Int( byte[] hex ) { 134 136 int len; 139 if(hex.length < 4 ) return 0; 140 if( DEC[hex[0]]<0 ) 141 throw new IllegalArgumentException (sm.getString("hexUtil.bad")); 142 len = DEC[hex[0]]; 143 len = len << 4; 144 if( DEC[hex[1]]<0 ) 145 throw new IllegalArgumentException (sm.getString("hexUtil.bad")); 146 len += DEC[hex[1]]; 147 len = len << 4; 148 if( DEC[hex[2]]<0 ) 149 throw new IllegalArgumentException (sm.getString("hexUtil.bad")); 150 len += DEC[hex[2]]; 151 len = len << 4; 152 if( DEC[hex[3]]<0 ) 153 throw new IllegalArgumentException (sm.getString("hexUtil.bad")); 154 len += DEC[hex[3]]; 155 return len; 156 } 157 158 159 160 166 private static char convertDigit(int value) { 167 168 value &= 0x0f; 169 if (value >= 10) 170 return ((char) (value - 10 + 'a')); 171 else 172 return ((char) (value + '0')); 173 174 } 175 176 177 } 178 | Popular Tags |