Android Hex String to Byte Array Convert decodeHex(String str)

Here you can find the source of decodeHex(String str)

Description

decode Hex

License

Open Source License

Declaration

public static byte[] decodeHex(String str) 

Method Source Code

//package com.java2s;
/*//ww w . j  a va 2  s .c  om
 * Created by Angel Leon (@gubatron), Alden Torres (aldenml)
 * Copyright (c) 2011-2014, FrostWire(R). All rights reserved.
 *
 * 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/>.
 */

import java.util.Locale;

public class Main {
    public static byte[] decodeHex(String str) {
        str = str.toLowerCase(Locale.US);
        int len = str.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(str.charAt(i), 16) << 4) + Character
                    .digit(str.charAt(i + 1), 16));
        }
        return data;
    }
}

Related

  1. hexStr2Str(String hexStr)
  2. toByte(String hexString)
  3. toBytesFromHexString(String digits)
  4. hexStringToBytes(String s)
  5. hexStringToBytes(String s)
  6. fromHex(String hex)
  7. hexStringToByteArray(String s)