1 19 package gcc.util; 20 21 public class Base16Binary 22 { 23 26 public static byte[] fromString(String string) 27 { 28 return fromString(string, 0, string.length()); 29 } 30 31 34 public static byte[] fromString(String string, int offset, int length) 35 { 36 byte[] bytes = new byte[length / 2]; 37 for (int j = 0, k = 0; k < length; j++, k += 2) 38 { 39 int hi = Character.digit(string.charAt(offset + k), 16); 40 int lo = Character.digit(string.charAt(offset + k + 1), 16); 41 if (hi == -1 || lo == -1) 42 { 43 throw new IllegalArgumentException(string); 44 } 45 bytes[j] = (byte)(16 * hi + lo); 46 } 47 return bytes; 48 } 49 50 53 public static String toString(byte[] bytes) 54 { 55 return toString(bytes, 0, bytes.length); 56 } 57 58 61 public static String toString(byte[] bytes, int offset, int length) 62 { 63 char[] chars = new char[length * 2]; 64 for (int j = 0, k = 0; j < length; j++, k += 2) 65 { 66 int value = (bytes[offset + j] + 256) & 255; 67 chars[k] = Character.forDigit(value >> 4, 16); 68 chars[k + 1] = Character.forDigit(value & 15, 16); 69 } 70 return new String(chars); 71 } 72 } 73 | Popular Tags |