Java tutorial
//package com.java2s; /** * Source obtained from crypto-gwt. Apache 2 License. * https://code.google.com/p/crypto-gwt/source/browse/crypto-gwt/src/main/java/com/googlecode/ * cryptogwt/util/ByteArrayUtils.java */ public class Main { public static byte[] hexToBytes(String hex) { hex = removeSpaces(hex); // Remove spaces assert hex.length() % 2 == 0 : "must be even number of bytes"; int resultLen = hex.length() / 2; byte[] result = new byte[resultLen]; int j = 0; for (int i = 0; i < resultLen; i++) { result[i] = (byte) (Byte.parseByte(hex.substring(j, ++j), 16) << 4 | Byte.parseByte(hex.substring(j, ++j), 16)); } return result; } private static String removeSpaces(String string) { string = string.replaceAll("\\s+", ""); return string; } }