我有這樣的僞代碼在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