@Test(expected = IllegalArgumentException.class) public void testOddLength() { String hexString = "AAB"; UperEncoder.bytesFromHexString(hexString); fail("Expected exception."); }
public static byte[] bytesFromHexString(String s) { if ((s.length() % 2) != 0) { throw new IllegalArgumentException( "Converting to bytes requires even number of characters, got " + s.length()); } int len = s.length(); byte[] data = new byte[len / 2]; for (int i = 0; i < len; i += 2) { data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16)); } return data; }
UperEncoder { public static byte[] bytesFromHexString(String s) { if ((s.length() % 2) != 0) { throw new IllegalArgumentException( "Converting to bytes requires even number of characters, got " + s.length()); } int len = s.length(); byte[] data = new byte[len / 2]; for (int i = 0; i < len; i += 2) { data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16)); } return data; } }
UperEncoder { public static byte[] bytesFromHexString(String s) { if ((s.length() % 2) != 0) { throw new IllegalArgumentException( "Converting to bytes requires even number of characters, got " + s.length()); } int len = s.length(); byte[] data = new byte[len / 2]; for (int i = 0; i < len; i += 2) { data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16)); } return data; } private  UperEncoder(); }
UperEncoder { public static byte[] bytesFromHexString(String s) { if ((s.length() % 2) != 0) { throw new IllegalArgumentException( "Converting to bytes requires even number of characters, got " + s.length()); } int len = s.length(); byte[] data = new byte[len / 2]; for (int i = 0; i < len; i += 2) { data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16)); } return data; } private  UperEncoder(); static byte[] encode(T obj); static T decode(byte[] bytes, Class<T> classOfT); static byte[] bytesFromCollection(List<Boolean> bitlist); static String hexStringFromBytes(byte[] bytes); static byte[] bytesFromHexString(String s); static String binaryStringFromBytes(byte[] bytes); static byte[] bytesFromBinaryString(String s); }
UperEncoder { public static byte[] bytesFromHexString(String s) { if ((s.length() % 2) != 0) { throw new IllegalArgumentException( "Converting to bytes requires even number of characters, got " + s.length()); } int len = s.length(); byte[] data = new byte[len / 2]; for (int i = 0; i < len; i += 2) { data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16)); } return data; } private  UperEncoder(); static byte[] encode(T obj); static T decode(byte[] bytes, Class<T> classOfT); static byte[] bytesFromCollection(List<Boolean> bitlist); static String hexStringFromBytes(byte[] bytes); static byte[] bytesFromHexString(String s); static String binaryStringFromBytes(byte[] bytes); static byte[] bytesFromBinaryString(String s);  }