Android Hex String to Byte Array Convert hexToBytes(String hexString)

Here you can find the source of hexToBytes(String hexString)

Description

Takes a string in hexidecimal format and converts it to a binary byte array.

License

Open Source License

Declaration

public static byte[] hexToBytes(String hexString) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from   w w  w .  j  ava2  s.c  om
     * Takes a string in hexidecimal format and converts it to a binary byte
     * array. This does no checking of the format of the input, so this should
     * only be used after confirming the format or origin of the string. The input
     * string should only contain the hex data, two characters per byte.
     */
    public static byte[] hexToBytes(String hexString) {
        byte[] result = new byte[hexString.length() / 2];
        for (int i = 0; i < result.length; ++i) {
            int offset = i * 2;
            result[i] = (byte) Integer.parseInt(
                    hexString.substring(offset, offset + 2), 16);
        }
        return result;
    }
}

Related

  1. hex2byte(String inputString)
  2. fromHex(String hex)
  3. fromHex(String hex)
  4. hexToBytes(String str)
  5. hexToByte(String input)
  6. hexToByte(char char1, char char2)
  7. hexStringToByteArray(String s)
  8. hexStringToBytes(String s)
  9. hex2bytes(String hex)