I have this pseudo-code in java:
bytes[] hash = MD5.hash("example");
String hexString = toHexString(hash); //This returns something like a0394dbe93f
bytes[] hexBytes = hexString.getBytes("UTF-8");
Now, hexBytes[]
and hash[]
are different.
I know I'm doing something wrong since hash.length()
is 16 and hexBytes.length()
is 32. Maybe it has something to do with java using Unicode for chars (just a wild guess here).
Anyways, the question would be: how to get the original hash[]
array from the hexString
.
The whole code is here if you want to look at it (it's ~ 40 LOC) http://gist.github.com/434466
The output of that code is:
16
[-24, 32, -69, 74, -70, 90, -41, 76, 90, 111, -15, -84, -95, 102, 65, -10]
32
[101, 56, 50, 48, 98, 98, 52, 97, 98, 97, 53, 97, 100, 55, 52, 99, 53, 97, 54, 102, 102, 49, 97, 99, 97, 49, 54, 54, 52, 49, 102, 54]
Thanks a lot!
You are just getting the bytes of the hex string with hexString.getBytes("UTF-8");
, not converting the hex digits to their byte values.
That is, you need to write the reverse of your toHexString
function.
Your toHexString should probably make sure to format values below 10 to 2 digits, so e.g. the byte 9 ends up as "09" and not "9".