represent MD5 as a number in base36

H

Henry Townsend

What's the most efficient/elegant way to represent an MD5 in base 36?
Here's some code I've cribbed together from MD5 samples but this is
converting byte-by-byte to base 16 then on to base 36, which seems dumb.
But I'm kind of lost in all the different constructors and static
methods offered by Byte, String, Long, etc.

byte[] digest = md.digest();
StringBuffer hex = new StringBuffer();
for (byte b : digest) {
hex.append(Integer.toHexString(b & 0xff));
}
String base36 = new BigInteger(hex.toString(), 16).toString(36);

Thanks,
HT
 
T

Thomas Hawtin

Henry said:
What's the most efficient/elegant way to represent an MD5 in base 36?
Here's some code I've cribbed together from MD5 samples but this is
converting byte-by-byte to base 16 then on to base 36, which seems dumb.
But I'm kind of lost in all the different constructors and static
methods offered by Byte, String, Long, etc.

byte[] digest = md.digest();
StringBuffer hex = new StringBuffer();
for (byte b : digest) {
hex.append(Integer.toHexString(b & 0xff));
^ This wont work for b between 0 and 15.
}
String base36 = new BigInteger(hex.toString(), 16).toString(36);

Can you not use the new BigInteger(int,byte[]) constructor?

Tom Hawtin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top