@Test public void toBytes() throws Exception { for(String string : strings) { byte [] bytes = Hex.decode(string); assertArrayEquals(bytes, HexUtils.toBytes(string)); } }
public static byte[] toBytes(String hexString) { if(hexString != null) { hexString = hexString.replace(" ", ""); } if (hexString == null || hexString.length() % 2 != 0) { throw new RuntimeException("Input string must contain an even number of characters"); } char[] hex = hexString.toCharArray(); int length = hex.length / 2; byte[] raw = new byte[length]; for (int i = 0; i < length; i++) { int high = Character.digit(hex[i * 2], 16); int low = Character.digit(hex[i * 2 + 1], 16); if (high < 0 || low < 0){ throw new RuntimeException("Invalid hex digit " + hex[i * 2] + hex[i * 2 + 1]); } int value = (high << 4) | low; if (value > 127) value -= 256; raw[i] = (byte) value; } return raw; }
HexUtils { public static byte[] toBytes(String hexString) { if(hexString != null) { hexString = hexString.replace(" ", ""); } if (hexString == null || hexString.length() % 2 != 0) { throw new RuntimeException("Input string must contain an even number of characters"); } char[] hex = hexString.toCharArray(); int length = hex.length / 2; byte[] raw = new byte[length]; for (int i = 0; i < length; i++) { int high = Character.digit(hex[i * 2], 16); int low = Character.digit(hex[i * 2 + 1], 16); if (high < 0 || low < 0){ throw new RuntimeException("Invalid hex digit " + hex[i * 2] + hex[i * 2 + 1]); } int value = (high << 4) | low; if (value > 127) value -= 256; raw[i] = (byte) value; } return raw; } }
HexUtils { public static byte[] toBytes(String hexString) { if(hexString != null) { hexString = hexString.replace(" ", ""); } if (hexString == null || hexString.length() % 2 != 0) { throw new RuntimeException("Input string must contain an even number of characters"); } char[] hex = hexString.toCharArray(); int length = hex.length / 2; byte[] raw = new byte[length]; for (int i = 0; i < length; i++) { int high = Character.digit(hex[i * 2], 16); int low = Character.digit(hex[i * 2 + 1], 16); if (high < 0 || low < 0){ throw new RuntimeException("Invalid hex digit " + hex[i * 2] + hex[i * 2 + 1]); } int value = (high << 4) | low; if (value > 127) value -= 256; raw[i] = (byte) value; } return raw; }  }
HexUtils { public static byte[] toBytes(String hexString) { if(hexString != null) { hexString = hexString.replace(" ", ""); } if (hexString == null || hexString.length() % 2 != 0) { throw new RuntimeException("Input string must contain an even number of characters"); } char[] hex = hexString.toCharArray(); int length = hex.length / 2; byte[] raw = new byte[length]; for (int i = 0; i < length; i++) { int high = Character.digit(hex[i * 2], 16); int low = Character.digit(hex[i * 2 + 1], 16); if (high < 0 || low < 0){ throw new RuntimeException("Invalid hex digit " + hex[i * 2] + hex[i * 2 + 1]); } int value = (high << 4) | low; if (value > 127) value -= 256; raw[i] = (byte) value; } return raw; }  static String toHex(byte[] bytes); static String toHex(byte[] bytes, String separator); static String toHex(byte[] bytes, int offset, int length); static String toHex(byte b); static String toHex(byte[] bytes, int offset, int length, String separator); static byte[] toBytes(String hexString); static void appendByteAsHex(StringBuilder sb, byte b); static boolean isAllZero(byte[] bytes); }
HexUtils { public static byte[] toBytes(String hexString) { if(hexString != null) { hexString = hexString.replace(" ", ""); } if (hexString == null || hexString.length() % 2 != 0) { throw new RuntimeException("Input string must contain an even number of characters"); } char[] hex = hexString.toCharArray(); int length = hex.length / 2; byte[] raw = new byte[length]; for (int i = 0; i < length; i++) { int high = Character.digit(hex[i * 2], 16); int low = Character.digit(hex[i * 2 + 1], 16); if (high < 0 || low < 0){ throw new RuntimeException("Invalid hex digit " + hex[i * 2] + hex[i * 2 + 1]); } int value = (high << 4) | low; if (value > 127) value -= 256; raw[i] = (byte) value; } return raw; }  static String toHex(byte[] bytes); static String toHex(byte[] bytes, String separator); static String toHex(byte[] bytes, int offset, int length); static String toHex(byte b); static String toHex(byte[] bytes, int offset, int length, String separator); static byte[] toBytes(String hexString); static void appendByteAsHex(StringBuilder sb, byte b); static boolean isAllZero(byte[] bytes);  }