Convert hex String to Byte array using Byte.decode method - Android java.lang

Android examples for java.lang:Byte Array

Description

Convert hex String to Byte array using Byte.decode method

Demo Code



public class Main{

    public static byte[] hexStr2Bytes(String src) {
        int m = 0, n = 0;
        int l = src.length() / 2;
        byte[] ret = new byte[l];
        for (int i = 0; i < l; i++) {
            m = i * 2 + 1;//w  ww .j  a  v  a 2  s .  c  om
            n = m + 1;
            ret[i] = Byte.decode("0x" + src.substring(i * 2, m)
                    + src.substring(m, n));
        }
        return ret;
    }

}

Related Tutorials