我有这样的伪代码在java中:从字符串为十六进制的MD5哈希和背部
bytes[] hash = MD5.hash("example");
String hexString = toHexString(hash); //This returns something like a0394dbe93f
bytes[] hexBytes = hexString.getBytes("UTF-8");
现在,hexBytes[]
和hash[]
是不同的。
我知道我做错了什么,因为hash.length()
是16,而hexBytes.length()
是32.也许它与java使用Unicode的字符有关(这里只是一个疯狂的猜测)。
不管怎么说,问题是:如何从hexString
获得原始hash[]
数组。
整个代码是在这里,如果你想看看它(这是〜40 LOC)http://gist.github.com/434466
代码的输出是:
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]
非常感谢!
代码是我挂HTTP要点://gist.github .com/434466(尽管名称不同)。谢谢,我会考虑Apache Commons – 2010-06-11 13:49:02
只是好奇......你为什么说API不理想? – 2010-06-11 13:55:58
@Pablo:理想情况下,应该有一个采用 字符串并返回强类型的字节数组的Hex.decode方法。 “Object decode(Object)'签名很烦人。 – 2010-06-11 14:26:16