How can I convert a char array of hex to byte array in Java? I don't want to convert char array to string for security reasons.
Is there any inbuilt library available for this conversion in Java 8?
How can I convert a char array of hex to byte array in Java? I don't want to convert char array to string for security reasons.
Is there any inbuilt library available for this conversion in Java 8?
There is no single Java SE method for it, but with Character.digit it’s fairly straightforward:
byte[] parse(char[] hex) {
int len = hex.length;
if (len % 2 != 0) {
throw new IllegalArgumentException(
"Even number of digits required");
}
byte[] bytes = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
int high = Character.digit(hex[i], 16);
int low = Character.digit(hex[i + 1], 16);
bytes[i / 2] = (byte) (high << 4 | low);
}
return bytes;
}