Here you can find the source of toByteArray(String hex)
public static byte[] toByteArray(String hex)
//package com.java2s; /**/*from w w w.ja v a2s . c o m*/ * Copyright 2001-2014 CryptoHeaven Corp. All Rights Reserved. * * This software is the confidential and proprietary information * of CryptoHeaven Corp. ("Confidential Information"). You * shall not disclose such Confidential Information and shall use * it only in accordance with the terms of the license agreement * you entered into with CryptoHeaven Corp. */ public class Main { /** data for hexadecimal visualisation. */ private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; /** * Returns a byte array from a String containing hex encoding bytes. */ public static byte[] toByteArray(String hex) { byte[] bytes = null; if (hex != null) { bytes = new byte[hex.length() / 2]; for (int i = 0; i < hex.length(); i += 2) { char ch1 = hex.charAt(i); char ch2 = hex.charAt(i + 1); bytes[i / 2] = (byte) hexCharsToByte(ch1, ch2); } } return bytes; } public static int hexCharsToByte(char hexDigit1, char hexDigit2) { int b1 = 0; int b2 = 0; char ch1 = hexDigit1; char ch2 = hexDigit2; if (ch1 >= HEX_DIGITS[0] && ch1 <= HEX_DIGITS[9]) { b1 = (int) (ch1 - HEX_DIGITS[0]); } else { b1 = (int) (ch1 - HEX_DIGITS[10] + 10); } if (ch2 >= HEX_DIGITS[0] && ch2 <= HEX_DIGITS[9]) { b2 = (int) (ch2 - HEX_DIGITS[0]); } else { b2 = (int) (ch2 - HEX_DIGITS[10] + 10); } return (int) ((b1 << 4) | b2); } }