Convert hex String To Byte Array via Character.digit - Android java.lang

Android examples for java.lang:Byte Array

Description

Convert hex String To Byte Array via Character.digit

Demo Code

public class Main {

  public static byte[] hexStringToByteArray(String s) {
    int len = s.length();
    byte[] d = new byte[len / 2];
    for (int i = 0; i < len; i += 2) {

      d[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16));
    }/*from ww w.j ava 2s.c om*/
    return d;
  }

}

Related Tutorials