Convert hex String To Byte Array using for loop - Android java.lang

Android examples for java.lang:Byte Array

Description

Convert hex String To Byte Array using for loop

Demo Code


public class Main{

    public static byte[] hexStringToByteArray(String str) {
        int len = str.length();
        byte[] b = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            b[i / 2] = (byte) ((Character.digit(str.charAt(i), 16) << 4) + Character
                    .digit(str.charAt(i + 1), 16));
        }//from w w w .  j a v  a 2  s  . com
        return b;
    }

}

Related Tutorials