I am looking for a way to convert string that represents hex values into a byte array. This code is perfect working for even number string length but not for odd number string length. If string length is odd then I get java.lang.StringIndexOutOfBoundsException. Please help.
public byte[] hexStringToByteArray(String s) {
byte[] data = null;
if(! s.equals("")){
int len = s.length();
data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
}
return data;
}
String#getBytes()?