static public byte [ ] convertStringToByteArr ( String s ) { if ( ( s . length ( ) % 2 ) != 0 ) { throw new RuntimeException ( "String length must be even (was " + s . length ( ) + ")" ) ; } ArrayList < Byte > byteArrayList = new ArrayList < Byte > ( ) ; for ( int i = 0 ; i < s . length ( ) ; i = i + 2 ) { String s2 = s . substring ( i , i + 2 ) ; Integer i2 = Integer . parseInt ( s2 , 16 ) ; Byte b2 = ( byte ) ( i2 & 0xff ) ; byteArrayList . add ( b2 ) ; } byte [ ] byteArr = new byte [ byteArrayList . size ( ) ] ; for ( int i = 0 ; i < byteArr . length ; i ++ ) { byteArr [ i ] = byteArrayList . get ( i ) ; } return byteArr ; }
Hexadecimal string to brute - force convert into an array of bytes . The length of the string must be even . The length of the string is 2x the length of the byte array .