Solved

# Simple md5 question - leading zeros

Posted on 2004-09-19
472 Views
hi,

I have come across this code at many places on net

StringBuffer hexString = new StringBuffer();
for (int i=0; i < digest.length; i++) {
String hex = Integer.toHexString(0xFF & digest[i]);
if (hex.length() == 1) {
hexString.append("0" + hex);
} else {
hexString.append(hex);
}
}

my question is -- what's the use of doing
0xFF & digest[i]

Thnx...
0
Question by:kumvjuec
• 4
• 4

LVL 86

Expert Comment

That would be to 'unsign' the byte value, which of course is a signed type
0

LVL 3

Author Comment

thnx cehj, can u explain li'l more...

how can a byte value be signed.. and how this operation will make difference
0

LVL 86

Expert Comment

It's actually not only to unsign it - it's to ensure the value does not exceed 0xFF after being passed to Integer.toHexString. Let's assume that digest[i] is itself 0xFF (-1)

Integer.toHexString(0xFF)

would produce

"ffffffff"

because of 'sign extension'. ANDing 0xFF with 0xFF would produce as a hex int

000000FF, and when that int is passed to Integer.toHexString, you'd get

"ff", which is what you want
0

LVL 3

Author Comment

cehj,
can u also give me a code to demonstrate this. I tried
"System.out" ing the Integer.toHexString(0xff) and Integer.toHexString(0xff & 0xff)
but both are printing out the same thing
i.e 0xff

Thanks,
0

LVL 3

Author Comment

sorry, they are printing out ff instead.
0

LVL 86

Accepted Solution

CEHJ earned 125 total points
That's because you're using an integer literal instead of a byte and those values will not be sign extended. Try this

byte b = (byte)0x80; // (-128)
System.out.println(Integer.toHexString(b));
0

LVL 3

Author Comment

thnx cehj
0

LVL 86

Expert Comment

8-)

Rather than calling Integer.toHexString, you may find the following more efficient:

System.out.println(byteArrayToHexString(new byte[] { (byte)0xca, (byte)0xfe, (byte)0xba, (byte)0xbe}));

...........

public static String byteArrayToHexString(byte[] rawBytes) {
String charIndex = "0123456789abcdef";
StringBuffer sb = new StringBuffer(rawBytes.length * 2);
for (int i = 0; i < rawBytes.length; i++) {
int ix = (rawBytes[i] >> 4) & 0xF;
sb.append(charIndex.charAt(ix));
ix = rawBytes[i] & 0xF;
sb.append(charIndex.charAt(ix));
}
return sb.toString();
}
0

## Featured Post

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.