Tuesday, October 2, 2007

java code hexToByte

    private static final String HEXINDEX = "0123456789abcdef          ABCDEF";

public static byte[] hexToByte(String s) {
int l = s.length() / 2;
byte data[] = new byte[l];
int j = 0;

for (int i = 0; i < l; i++) {
char c = s.charAt(j++);
int n, b;

n = HEXINDEX.indexOf(c);
b = (n & 0xf) << 4;
c = s.charAt(j++);
n = HEXINDEX.indexOf(c);
b += (n & 0xf);
data[i] = (byte) b;
}
return data;
}

2 comments:

Unknown said...

I looked all over for something that does exactly what you have here. This works perfect. Thank you very much.

Unknown said...
This comment has been removed by the author.