static final byte [ ] fromHex ( final String hex ) throws ParseException { final int len = hex . length ( ) ; final byte [ ] out = new byte [ len / 2 ] ; for ( int i = 0 , j = 0 ; i < len ; i ++ ) { char c = hex . charAt ( i ) ; int v = 0 ; if ( ( c >= ' ' ) && ( c <= ' ' ) ) { v = ( c - ' ' ) ; } else if ( ( c >= ' ' ) && ( c <= ' ' ) ) { v = ( c - ' ' ) + 0xA ; } else if ( ( c >= ' ' ) && ( c <= ' ' ) ) { v = ( c - ' ' ) + 0xA ; } else { throw new ParseException ( "Invalid char" , j ) ; } if ( ( i & 1 ) == 0 ) { out [ j ] |= ( v << 4 ) ; } else { out [ j ++ ] |= v ; } } return out ; }
Transform Hex String to byte array