Java Byte Array Create byteArrayFromHexString(String in)

Here you can find the source of byteArrayFromHexString(String in)

Description

Convert an hex-string to byte array "0001a0" -> {0x00, 0x01, 0xa0}

License

Open Source License

Parameter

Parameter Description
in a hexadecimal string

Return

a byte array

Declaration

public static byte[] byteArrayFromHexString(String in) 

Method Source Code

//package com.java2s;
/******************************************************************************
 *
 * Copyright (c) 2005-2011 Cryptzone Group AB. All Rights Reserved.
 * //  ww w .jav a2  s  . c o  m
 * This file contains Original Code and/or Modifications of Original Code as
 * defined in and that are subject to the MindTerm Public Source License,
 * Version 2.0, (the 'License'). You may not use this file except in compliance
 * with the License.
 * 
 * You should have received a copy of the MindTerm Public Source License
 * along with this software; see the file LICENSE.  If not, write to
 * Cryptzone Group AB, Drakegatan 7, SE-41250 Goteborg, SWEDEN
 *
 *****************************************************************************/

public class Main {
    /**
     * Convert an hex-string to byte array "0001a0" -> {0x00, 0x01, 0xa0}
     *
     * @param in a hexadecimal string
     * @return a byte array
     */
    public static byte[] byteArrayFromHexString(String in) {
        byte out[] = new byte[in.length() / 2];
        for (int i = 0; i < in.length(); i += 2) {
            out[i / 2] = (byte) Integer.parseInt(in.substring(i, i + 2), 16);
        }
        return out;
    }
}

Related

  1. byteArrayFromBytes(int... bytes)
  2. byteArrayFromChar(char i)
  3. byteArrayFromInt(final int number)
  4. byteArrayFromInteger(int integer)
  5. byteArrayFromInteger(int integer)
  6. bytes(final byte... elements)