Android Hex String to Byte Array Convert hexStringToBytes(String hex)

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

Description

hex String To Bytes

Declaration

public static byte[] hexStringToBytes(String hex) 

Method Source Code

//package com.java2s;

public class Main {
    public static byte[] hexStringToBytes(String hex) {
        if (0 != hex.length() % 2)
            throw new IllegalArgumentException(hex
                    + " not be a valid HEX String!");

        byte[] result = new byte[hex.length() >> 1];

        int high_nibble;
        int low_nibble;

        for (int i = 0, j = 0; i < result.length; i++, j += 2) {
            high_nibble = Character.digit(hex.charAt(j), 16) << 4;
            low_nibble = Character.digit(hex.charAt(j + 1), 16);

            result[i] = (byte) (high_nibble + low_nibble);
        }/*from w  w  w . ja va  2  s  .  c  o  m*/

        return result;
    }
}

Related

  1. fromHex(String hex)
  2. fromHex(String hexData)
  3. hexStringToByteArray(String s)
  4. hexStringToBytes(String hex)
  5. hexStringToBytes(String hexString)
  6. hexStringToBytes(String hexString)
  7. toByte(String hexString)
  8. toByte(String hexString)