Android String to Byte Array Convert hexStringToByteArray(String hexa)

Here you can find the source of hexStringToByteArray(String hexa)

Description

hex String To Byte Array

Declaration

public static byte[] hexStringToByteArray(String hexa)
        throws IllegalArgumentException 

Method Source Code

//package com.java2s;

public class Main {
    private static final String hexDigits = "0123456789abcdef";

    public static byte[] hexStringToByteArray(String hexa)
            throws IllegalArgumentException {

        //verifica se a String possui uma quantidade par de elementos  
        if (hexa.length() % 2 != 0) {
            throw new IllegalArgumentException("String hexa inv?lida");
        }/*w w  w  .ja va 2s  .com*/

        byte[] b = new byte[hexa.length() / 2];

        for (int i = 0; i < hexa.length(); i += 2) {
            b[i / 2] = (byte) ((hexDigits.indexOf(hexa.charAt(i)) << 4) | (hexDigits
                    .indexOf(hexa.charAt(i + 1))));
        }
        return b;
    }
}

Related

  1. fromString(String binary)
  2. fromString(String str)
  3. toByte(String hexString)
  4. toByte(String hexString)
  5. toBytes(final String str)
  6. StringtoByte(String data)