Java Hex to Byte Array convertHexToByteArray(String hexString)

Here you can find the source of convertHexToByteArray(String hexString)

Description

convert Hex To Byte Array

License

Open Source License

Declaration

public static byte[] convertHexToByteArray(String hexString) 

Method Source Code

//package com.java2s;
/**/*from  w w  w  .j  av  a2 s .co  m*/
 * Project: Platforms for Collaboration at the AMMRF
 *
 * Copyright (c) Intersect Pty Ltd, 2011
 *
 * @see http://www.ammrf.org.au
 * @see http://www.intersect.org.au
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * This program contains open source third party libraries from a number of
 * sources, please read the THIRD_PARTY.txt file for more details.
 */

public class Main {
    private static final int HEX_BASE = 16;

    public static byte[] convertHexToByteArray(String hexString) {
        if ((hexString.length() % 2) != 0) {
            throw new IllegalArgumentException();
        }

        byte[] result = new byte[hexString.length() / 2];
        char[] enc = hexString.toCharArray();
        for (int i = 0; i < enc.length; i += 2) {
            StringBuilder curr = new StringBuilder(2);
            curr.append(enc[i]).append(enc[i + 1]);
            result[i / 2] = (byte) Integer.parseInt(curr.toString(),
                    HEX_BASE);
        }
        return result;
    }
}

Related

  1. convertHexStringToByteArray(String s)
  2. convertHexStringToByteArray(String str, int numBytes, int numCharsPerByte)
  3. convertHexStringToByteNoSpace(String s)
  4. convertHexStringToBytes(String hex)
  5. convertHexStringToBytes(String hex)
  6. convertHexToBytes(String s)
  7. convertHexToBytes(String s)