Android Hex String to Byte Array Convert hexStringToByteArray(String s)

Here you can find the source of hexStringToByteArray(String s)

Description

hex String To Byte Array

License

Open Source License

Declaration

static public byte[] hexStringToByteArray(String s) 

Method Source Code

//package com.java2s;
/**//from w  w w.  j a v a 2  s  . com
 * Logback: the reliable, generic, fast and flexible logging framework.
 * Copyright (C) 1999-2013, QOS.ch. All rights reserved.
 *
 * This program and the accompanying materials are dual-licensed under
 * either the terms of the Eclipse Public License v1.0 as published by
 * the Eclipse Foundation
 *
 *   or (per the licensee's choosing)
 *
 * under the terms of the GNU Lesser General Public License version 2.1
 * as published by the Free Software Foundation.
 */

public class Main {
    static public byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] ba = new byte[len / 2];

        for (int i = 0; i < ba.length; i++) {
            int j = i * 2;
            int t = Integer.parseInt(s.substring(j, j + 2), 16);
            byte b = (byte) (t & 0xFF);
            ba[i] = b;
        }
        return ba;
    }
}

Related

  1. toBytesFromHexString(String digits)
  2. hexStringToBytes(String s)
  3. hexStringToBytes(String s)
  4. decodeHex(String str)
  5. fromHex(String hex)