Possible Duplicate:
Convert a string representation of a hex dump to a byte array using Java?
I got an MD5 String
de70d4de8c47385536c8e08348032c3b
and I need it as the Byte Values
DE 70 D4 DE 8C 47 38 55 36 C8 E0 83 48 03 2C 3B
This should be similar to Perls pack("H32);
function.
There are many ways to do this. Here is one:
public static void main(String[] args) {
String s = "de70d4de8c47385536c8e08348032c3b";
Matcher m = Pattern.compile("..").matcher(s);
List<Byte> bytes = new ArrayList<Byte>();
while (m.find())
bytes.add((byte) Integer.parseInt(m.group(), 16));
System.out.println(bytes);
}
Outputs (-34
== 0xde
):
[-34, 112, -44, -34, -116, 71, 56, 85, 54, -56, -32, -125, 72, 3, 44, 59]