public static byte[] fromHexString(String text){
  text=text.trim();
  if (text.length() % 2 != 0)   text="0" + text;
  int resLen=text.length() / 2;
  int loNibble, hiNibble;
  byte[] res=new byte[resLen];
  for (int i=0; i < resLen; i++) {
    int j=i << 1;
    hiNibble=charToNibble(text.charAt(j));
    loNibble=charToNibble(text.charAt(j + 1));
    if (loNibble == -1 || hiNibble == -1)     return null;
    res[i]=(byte)(hiNibble << 4 | loNibble);
  }
  return res;
}

public static byte [ ] fromHexString ( String text ) { text = text . trim ( ) ; if ( text . length ( ) % 2 != 0 ) text = "0" + text ; int resLen = text . length ( ) / 2 ; int loNibble , hiNibble ; byte [ ] res = new byte [ resLen ] ; for ( int i = 0 ; i < resLen ; i ++ ) { int j = i << 1 ; hiNibble = charToNibble ( text . charAt ( j ) ) ; loNibble = charToNibble ( text . charAt ( j + 1 ) ) ; if ( loNibble == - 1 || hiNibble == - 1 ) return null ; res [ i ] = ( byte ) ( hiNibble << 4 | loNibble ) ; } return res ; }
convert a string containing consecutive ( no inside whitespace ) hexadecimal digits into a corresponding byte array . if the number of digits is not even , a ' 0 ' will be appended in the front of the string prior to conversion . leading and trailing whitespace is ignored .
