Android Hex String to Byte Array Convert toBytesFromHexString(String digits)

Here you can find the source of toBytesFromHexString(String digits)

Description

to Bytes From Hex String

Declaration

public static byte[] toBytesFromHexString(String digits)
        throws IllegalArgumentException, NumberFormatException 

Method Source Code

//package com.java2s;

public class Main {

    public static byte[] toBytesFromHexString(String digits)
            throws IllegalArgumentException, NumberFormatException {
        if (digits == null) {
            return null;
        }/*from   ww  w. j  a  v a2  s.c  o m*/
        int length = digits.length();
        if (length % 2 == 1) {
            throw new IllegalArgumentException("For input string: \""
                    + digits + "\"");
        }
        length = length / 2;
        byte[] bytes = new byte[length];
        for (int i = 0; i < length; i++) {
            int index = i * 2;
            bytes[i] = (byte) (Short.parseShort(
                    digits.substring(index, index + 2), 16));
        }
        return bytes;
    }
}

Related

  1. hexStr2ByteArr(String paramString)
  2. hexStr2ByteArr(String str)
  3. hexStr2Bytes(String src)
  4. hexStr2Str(String hexStr)
  5. toByte(String hexString)
  6. hexStringToBytes(String s)
  7. hexStringToBytes(String s)
  8. decodeHex(String str)
  9. fromHex(String hex)