Java Byte Array Create toByteArray(String hex)

Here you can find the source of toByteArray(String hex)

Description

to Byte Array

License

Open Source License

Declaration

public static byte[] toByteArray(String hex) 

Method Source Code

//package com.java2s;
/*/*from  ww  w.j ava 2  s .co  m*/
 * $Header: Util.java, 14-Jul-2005 03:27:51 luisantonioa Exp $
 *
 * $Author: luisantonioa $ $Date: 14-Jul-2005 03:27:51 $ $Revision: 1 $ $Log:
 * Util.java,v $ Revision 1 14-Jul-2005 03:27:51 luisantonioa Added copyright
 * notice
 *
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * 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, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    public static byte[] toByteArray(String hex) {
        int len = (hex.length() + 1) / 3;
        byte[] data = new byte[len];
        for (int i = 0; i < len; i++) {
            data[i] = (byte) ((Character.digit(hex.charAt(i * 3), 16) << 4)
                    + Character.digit(hex.charAt(i * 3 + 1), 16));
        }
        return data;
    }
}

Related

  1. toByteArray(short foo)
  2. toByteArray(String address)
  3. toByteArray(String binString)
  4. toByteArray(String bytesStr, int byteLength)
  5. toByteArray(String content, String charsetName)
  6. toByteArray(String hex)
  7. toByteArray(String hex)
  8. toByteArray(String hex)
  9. toByteArray(String hexStr)