I know this question is asked a lot of times but please hear me out.
Previously I have tried following methods to convert hex string to byte array.
say my keyA = "D14E2C5A5B5F", I use byte[] of this key to authenticate a mifare card
First Approach:
byte[] ka = new BigInteger(keyA, 16).toByteArray();
(With this approach using ka as key authenticates few cards and fails in few cards)
Second Approach:
byte[] ka = hexStringToByteArray(keyA);
public byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] 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;
}
(With this approach using ka as key authenticates few cards, but success rate is more than first approach and fails in few cards).
Am I missing anything? Is there any better way to convert hex string to byte array in java?
Thanks in advance.