@Test public void testHexStringToByteArray() { assertThat(Numeric.hexStringToByteArray(""), is(new byte[] { })); assertThat(Numeric.hexStringToByteArray("0"), is(new byte[] { 0 })); assertThat(Numeric.hexStringToByteArray("1"), is(new byte[] { 0x1 })); assertThat(Numeric.hexStringToByteArray(HEX_RANGE_STRING), is(HEX_RANGE_ARRAY)); assertThat(Numeric.hexStringToByteArray("0x123"), is(new byte[] { 0x1, 0x23 })); }
public static byte[] hexStringToByteArray(String input) { String cleanInput = cleanHexPrefix(input); int len = cleanInput.length(); if (len == 0) { return new byte[] {}; } byte[] data; int startIdx; if (len % 2 != 0) { data = new byte[(len / 2) + 1]; data[0] = (byte) Character.digit(cleanInput.charAt(0), 16); startIdx = 1; } else { data = new byte[len / 2]; startIdx = 0; } for (int i = startIdx; i < len; i += 2) { data[(i + 1) / 2] = (byte) ((Character.digit(cleanInput.charAt(i), 16) << 4) + Character.digit(cleanInput.charAt(i + 1), 16)); } return data; }
Numeric { public static byte[] hexStringToByteArray(String input) { String cleanInput = cleanHexPrefix(input); int len = cleanInput.length(); if (len == 0) { return new byte[] {}; } byte[] data; int startIdx; if (len % 2 != 0) { data = new byte[(len / 2) + 1]; data[0] = (byte) Character.digit(cleanInput.charAt(0), 16); startIdx = 1; } else { data = new byte[len / 2]; startIdx = 0; } for (int i = startIdx; i < len; i += 2) { data[(i + 1) / 2] = (byte) ((Character.digit(cleanInput.charAt(i), 16) << 4) + Character.digit(cleanInput.charAt(i + 1), 16)); } return data; } }
Numeric { public static byte[] hexStringToByteArray(String input) { String cleanInput = cleanHexPrefix(input); int len = cleanInput.length(); if (len == 0) { return new byte[] {}; } byte[] data; int startIdx; if (len % 2 != 0) { data = new byte[(len / 2) + 1]; data[0] = (byte) Character.digit(cleanInput.charAt(0), 16); startIdx = 1; } else { data = new byte[len / 2]; startIdx = 0; } for (int i = startIdx; i < len; i += 2) { data[(i + 1) / 2] = (byte) ((Character.digit(cleanInput.charAt(i), 16) << 4) + Character.digit(cleanInput.charAt(i + 1), 16)); } return data; } private  Numeric(); }
Numeric { public static byte[] hexStringToByteArray(String input) { String cleanInput = cleanHexPrefix(input); int len = cleanInput.length(); if (len == 0) { return new byte[] {}; } byte[] data; int startIdx; if (len % 2 != 0) { data = new byte[(len / 2) + 1]; data[0] = (byte) Character.digit(cleanInput.charAt(0), 16); startIdx = 1; } else { data = new byte[len / 2]; startIdx = 0; } for (int i = startIdx; i < len; i += 2) { data[(i + 1) / 2] = (byte) ((Character.digit(cleanInput.charAt(i), 16) << 4) + Character.digit(cleanInput.charAt(i + 1), 16)); } return data; } private  Numeric(); static String encodeQuantity(BigInteger value); static BigInteger decodeQuantity(String value); static String cleanHexPrefix(String input); static String prependHexPrefix(String input); static boolean containsHexPrefix(String input); static BigInteger toBigInt(byte[] value, int offset, int length); static BigInteger toBigInt(byte[] value); static BigInteger toBigInt(String hexValue); static BigInteger toBigIntNoPrefix(String hexValue); static String toHexStringWithPrefix(BigInteger value); static String toHexStringNoPrefix(BigInteger value); static String toHexStringNoPrefix(byte[] input); static String toHexStringWithPrefixZeroPadded(BigInteger value, int size); static String toHexStringWithPrefixSafe(BigInteger value); static String toHexStringNoPrefixZeroPadded(BigInteger value, int size); static byte[] toBytesPadded(BigInteger value, int length); static byte[] hexStringToByteArray(String input); static String toHexString(byte[] input, int offset, int length, boolean withPrefix); static String toHexString(byte[] input); static byte asByte(int m, int n); static boolean isIntegerValue(BigDecimal value); }
Numeric { public static byte[] hexStringToByteArray(String input) { String cleanInput = cleanHexPrefix(input); int len = cleanInput.length(); if (len == 0) { return new byte[] {}; } byte[] data; int startIdx; if (len % 2 != 0) { data = new byte[(len / 2) + 1]; data[0] = (byte) Character.digit(cleanInput.charAt(0), 16); startIdx = 1; } else { data = new byte[len / 2]; startIdx = 0; } for (int i = startIdx; i < len; i += 2) { data[(i + 1) / 2] = (byte) ((Character.digit(cleanInput.charAt(i), 16) << 4) + Character.digit(cleanInput.charAt(i + 1), 16)); } return data; } private  Numeric(); static String encodeQuantity(BigInteger value); static BigInteger decodeQuantity(String value); static String cleanHexPrefix(String input); static String prependHexPrefix(String input); static boolean containsHexPrefix(String input); static BigInteger toBigInt(byte[] value, int offset, int length); static BigInteger toBigInt(byte[] value); static BigInteger toBigInt(String hexValue); static BigInteger toBigIntNoPrefix(String hexValue); static String toHexStringWithPrefix(BigInteger value); static String toHexStringNoPrefix(BigInteger value); static String toHexStringNoPrefix(byte[] input); static String toHexStringWithPrefixZeroPadded(BigInteger value, int size); static String toHexStringWithPrefixSafe(BigInteger value); static String toHexStringNoPrefixZeroPadded(BigInteger value, int size); static byte[] toBytesPadded(BigInteger value, int length); static byte[] hexStringToByteArray(String input); static String toHexString(byte[] input, int offset, int length, boolean withPrefix); static String toHexString(byte[] input); static byte asByte(int m, int n); static boolean isIntegerValue(BigDecimal value);  }