public static byte [ ] decodeHex ( final String value ) { // if string length is odd then throw exception if ( value . length ( ) % 2 != 0 ) { throw new NumberFormatException ( "odd number of characters in hex string" ) ; } byte [ ] bytes = new byte [ value . length ( ) / 2 ] ; for ( int i = 0 ; i < value . length ( ) ; i += 2 ) { bytes [ i / 2 ] = ( byte ) Integer . parseInt ( value . substring ( i , i + 2 ) , 16 ) ; } return bytes ; }
Decodes a hex string to a byte array . The hex string can contain either upper case or lower case letters .