public static byte [ ] hexToByte ( String s ) throws IOException { int l = s . length ( ) / 2 ; byte data [ ] = new byte [ l ] ; int j = 0 ; if ( s . length ( ) % 2 != 0 ) { throw new IOException ( "hexadecimal string with odd number of characters" ) ; } for ( int i = 0 ; i < l ; i ++ ) { char c = s . charAt ( j ++ ) ; int n , b ; n = HEXINDEX . indexOf ( c ) ; if ( n == - 1 ) { throw new IOException ( "hexadecimal string contains non hex character" ) ; } b = ( n & 0xf ) << 4 ; c = s . charAt ( j ++ ) ; n = HEXINDEX . indexOf ( c ) ; b += ( n & 0xf ) ; data [ i ] = ( byte ) b ; } return data ; }
Compacts a hexadecimal string into a byte array