1 18 package org.apache.geronimo.interop.util; 19 20 public class Base16Binary { 21 24 public static byte[] fromString(String string) { 25 return fromString(string, 0, string.length()); 26 } 27 28 31 public static byte[] fromString(String string, int offset, int length) { 32 byte[] bytes = new byte[length / 2]; 33 for (int j = 0, k = 0; k < length; j++, k += 2) { 34 int hi = Character.digit(string.charAt(offset + k), 16); 35 int lo = Character.digit(string.charAt(offset + k + 1), 16); 36 if (hi == -1 || lo == -1) { 37 throw new IllegalArgumentException (string); 38 } 39 bytes[j] = (byte) (16 * hi + lo); 40 } 41 return bytes; 42 } 43 44 47 public static String toString(byte[] bytes) { 48 return toString(bytes, 0, bytes.length); 49 } 50 51 54 public static String toString(byte[] bytes, int offset, int length) { 55 char[] chars = new char[length * 2]; 56 for (int j = 0, k = 0; j < length; j++, k += 2) { 57 int value = (bytes[offset + j] + 256) & 255; 58 chars[k] = Character.forDigit(value >> 4, 16); 59 chars[k + 1] = Character.forDigit(value & 15, 16); 60 } 61 return new String (chars); 62 } 63 } 64 | Popular Tags |