Android String to Hex Byte Array Convert fromHex(String dataString)

Here you can find the source of fromHex(String dataString)

Description

Decode a hexadecimal string to a bytearray.

License

Open Source License

Parameter

Parameter Description
dataString a hexadecimal string (The string length must be even.)

Return

Decoded bytearray (if dataString == "" then return null.)

Declaration

public static byte[] fromHex(String dataString) 

Method Source Code

//package com.java2s;
import java.math.BigInteger;

public class Main {
    /**//w  w  w.jav  a2  s. c o  m
     * Decode a hexadecimal string to a bytearray.
     * 
     * @param dataString a hexadecimal string (The string length must be even.)
     * @return Decoded bytearray (if dataString == "" then return null.)
     */
    public static byte[] fromHex(String dataString) {
        if (dataString.equals("")) {
            return null;
        }
        BigInteger dataPlus1BigInteger = new BigInteger("10" + dataString,
                16);
        byte[] dataPlus1Bytes = dataPlus1BigInteger.toByteArray();
        byte[] dataBytes = new byte[dataPlus1Bytes.length - 1];
        System.arraycopy(dataPlus1Bytes, 1, dataBytes, 0, dataBytes.length);
        return dataBytes;
    }
}

Related

  1. getBytes(String hexString)
  2. hexStringToBytes(String hexString)
  3. stringToHex(String ids)
  4. hexStringToBytes(String s)
  5. hexStringToBytes(String hexString, int offset, int count)
  6. fromHexString(String s)
  7. hexDecode(String data)