/** * Convert a String containing hex characters into an array of bytes with the binary representation * of the hex string * @param s Hex string. Length must be even because each pair of hex chars is converted into a byte. * @return byte array with binary representation of hex string. * @throws NumberFormatException */ public static byte[] hexStringToBytes(final String s) throws NumberFormatException { if (s.length() % 2 != 0) { throw new NumberFormatException("Hex representation of byte string does not have even number of hex chars: " + s); } final byte[] ret = new byte[s.length() / 2]; for (int i = 0; i < ret.length; ++i) { ret[i] = (byte) ((fromHexDigit(s.charAt(i * 2)) << 4) | fromHexDigit(s.charAt(i * 2 + 1))); } return ret; }
/** * Convert a String containing hex characters into an array of bytes with the binary representation * of the hex string * @param s Hex string. Length must be even because each pair of hex chars is converted into a byte. * @return byte array with binary representation of hex string. * @throws NumberFormatException */ public static byte[] hexStringToBytes(final String s) throws NumberFormatException { if (s.length() % 2 != 0) { throw new NumberFormatException("Hex representation of byte string does not have even number of hex chars: " + s); } final byte[] ret = new byte[s.length() / 2]; for (int i = 0; i < ret.length; ++i) { ret[i] = (byte) ((fromHexDigit(s.charAt(i * 2)) << 4) | fromHexDigit(s.charAt(i * 2 + 1))); } return ret; }
/** * Convert a String containing hex characters into an array of bytes with the binary representation * of the hex string * @param s Hex string. Length must be even because each pair of hex chars is converted into a byte. * @return byte array with binary representation of hex string. * @throws NumberFormatException */ public static byte[] hexStringToBytes(final String s) throws NumberFormatException { if (s.length() % 2 != 0) { throw new NumberFormatException("Hex representation of byte string does not have even number of hex chars: " + s); } final byte[] ret = new byte[s.length() / 2]; for (int i = 0; i < ret.length; ++i) { ret[i] = (byte) ((fromHexDigit(s.charAt(i * 2)) << 4) | fromHexDigit(s.charAt(i * 2 + 1))); } return ret; }