1 17 package org.apache.ws.jaxme.util; 18 19 21 public class HexBinary { 22 24 public static byte[] getClone(byte[] pHexBinary) { 25 byte[] result = new byte[pHexBinary.length]; 26 System.arraycopy(pHexBinary, 0, result, 0, pHexBinary.length); 27 return result; 28 } 29 30 33 public static byte[] decode(String pValue) { 34 if ((pValue.length() % 2) != 0) { 35 throw new IllegalArgumentException ("A HexBinary string must have even length."); 36 } 37 byte[] result = new byte[pValue.length() / 2]; 38 int j = 0; 39 for (int i = 0; i < pValue.length(); ) { 40 byte b; 41 char c = pValue.charAt(i++); 42 char d = pValue.charAt(i++); 43 if (c >= '0' && c <= '9') { 44 b = (byte) ((c - '0') << 4); 45 } else if (c >= 'A' && c <= 'F') { 46 b = (byte) ((c - 'A' + 10) << 4); 47 } else if (c >= 'a' && c <= 'f') { 48 b = (byte) ((c - 'a' + 10) << 4); 49 } else { 50 throw new IllegalArgumentException ("Invalid hex digit: " + c); 51 } 52 if (d >= '0' && d <= '9') { 53 b += (byte) (d - '0'); 54 } else if (d >= 'A' && d <= 'F') { 55 b += (byte) (d - 'A' + 10); 56 } else if (d >= 'a' && d <= 'f') { 57 b += (byte) (d - 'a' + 10); 58 } else { 59 throw new IllegalArgumentException ("Invalid hex digit: " + d); 60 } 61 result[j++] = b; 62 } 63 return result; 64 } 65 66 69 public static String encode(byte[] pHexBinary) { 70 StringBuffer result = new StringBuffer (); 71 for (int i = 0; i < pHexBinary.length; i++) { 72 byte b = pHexBinary[i]; 73 byte c = (byte) ((b & 0xf0) >> 4); 74 if (c <= 9) { 75 result.append((char) ('0' + c)); 76 } else { 77 result.append((char) ('A' + c - 10)); 78 } 79 c = (byte) (b & 0x0f); 80 if (c <= 9) { 81 result.append((char) ('0' + c)); 82 } else { 83 result.append((char) ('A' + c - 10)); 84 } 85 } 86 return result.toString(); 87 } 88 } 89 | Popular Tags |