MD5 <-> subString

Hi,

I have a little problem with MD5 in combination with subString.

MessageDigest md = MessageDigest.getInstance("MD5");

byte[] temp = (""+userID).getBytes();

md.update(temp);

String hash = (md.digest()).toString();

md.reset();

If userID is 512 then hash is [ B@182f0db

String dir = "D:\\temp\\"+hash.substring(0,2)+"\\"+hash.substring(2,4)+"\\";

If I try:

File temp = new File(dir);

temp.mkdirs();

I can get 2 different results:

D:\temp\[B\@a\

D:\temp\[ B\@1\.

Anyone any idea how to solve this?

Thx!

[605 byte] By [VOPa] at [2007-11-27 2:40:43]
# 1

> If userID is 512 then hash is [ B@182f0db

The toString() method of a byte array returns its internal memory address in a string form, but not its content. When you run the program for another time, it's very likely that the array is allocated at a different address in the computer memory, so the result will be different.

In order to get the content of the array, read http://www.exampledepot.com/egs/java.math/Bytes2Str.html for an example.

wangwja at 2007-7-12 3:03:51 > top of Java-index,Security,Cryptography...
# 2
Thx!everything is working now!
VOPa at 2007-7-12 3:03:51 > top of Java-index,Security,Cryptography...