Solved

Simple md5 question - leading zeros

Posted on 2004-09-19
8
485 Views
Last Modified: 2008-01-16
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
Comment
Question by:kumvjuec
  • 4
  • 4
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12096622
That would be to 'unsign' the byte value, which of course is a signed type
0
 
LVL 3

Author Comment

by:kumvjuec
ID: 12096633
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

by:CEHJ
ID: 12096856
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

by:kumvjuec
ID: 12096928
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Author Comment

by:kumvjuec
ID: 12096934
sorry, they are printing out ff instead.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
ID: 12097008
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

by:kumvjuec
ID: 12097020
thnx cehj
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12097062
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
micro services vs rest web services 16 86
Impossible to extract MSI from new JAVA releases 2 49
eclipse package explorer vs project explorer view 2 84
Apps blocked by Java 9 64
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now