Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 257027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:03:46+00:00 2026-05-11T22:03:46+00:00

I recently started looking at MD5 hashing (in Java) and while I’ve found algorithms

  • 0

I recently started looking at MD5 hashing (in Java) and while I’ve found algorithms and methods to help me accomplish that, I’m left wondering how it actually works.

For one, I found the following from this URL:

private static String convertToHex(byte[] data) {
    StringBuffer buf = new StringBuffer();
    for (int i = 0; i < data.length; i++) {
        int halfbyte = (data[i] >>> 4) & 0x0F;
        int two_halfs = 0;
        do {
            if ((0 <= halfbyte) && (halfbyte <= 9))
                buf.append((char) ('0' + halfbyte));
            else
                buf.append((char) ('a' + (halfbyte - 10)));
                halfbyte = data[i] & 0x0F;
            } while(two_halfs++ < 1);
        }
    return buf.toString();
}

I haven’t found any need to use bit-shifting in Java so I’m a bit rusty on that. Someone kind enough to illustrate (in simple terms) how exactly does the above code does the conversion? “>>>”?

I also found other solutions on StackOverflow, such as here and here, which uses BigInteger instead:

try {
   String s = "TEST STRING";
   MessageDigest md5 = MessageDigest.getInstance("MD5");
   md5.update(s.getBytes(),0,s.length());
   String signature = new BigInteger(1,md5.digest()).toString(16);
   System.out.println("Signature: "+signature);

} catch (final NoSuchAlgorithmException e) {
   e.printStackTrace();
}

Why does that work too, and which way is more efficient?

Thanks for your time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T22:03:46+00:00Added an answer on May 11, 2026 at 10:03 pm
    private static String convertToHex(byte[] data) {
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < data.length; i++) {
    

    Up till this point … just basic set up and starting a loop to go through all bytes in the array

            int halfbyte = (data[i] >>> 4) & 0x0F;
    

    bytes when converted to hex are two hex digits or 8 binary digits depending on what base you look at it in. The above statement shifts the high 4 bits down (>>> is unsigned right shift) and logical ANDs it with 0000 1111 so that the result is an integer equal to the high 4 bits of the byte (first hex digit).

    Say 23 was an input, this is 0001 0111 in binary. The shift makes and logical AND coverts this to 0000 0001.

            int two_halfs = 0;
            do {
    

    This just sets up the do/while loop to run twice

                if ((0 <= halfbyte) && (halfbyte <= 9))
                    buf.append((char) ('0' + halfbyte));
                else
                    buf.append((char) ('a' + (halfbyte - 10)));
    

    Here we’re displaying the actual hex digit, basically just using the zero or a character as a starting point and shifting up to the correct character. The first if statement covers all the digits 0-9, and the second covers all digits 10-15 (a-f in hex)

    Again, using our example 0000 0001 in decimal is equal to 1. We get caught in the upper if block and add 1 to the ‘0’ character to get the character ‘1’, append that to the string and move on.

                    halfbyte = data[i] & 0x0F;
    

    Now we set up the integer to just equal the low bits from the byte and repeat.

    Again, if our input was 23 … 0001 0111 after the logical AND becomes just 0000 0111 which is 7 in decimal. Repeat the same logic as above and the character ‘7’ is displayed.

                } while(two_halfs++ < 1);
    

    Now we just move on to the next byte in the array and repeat.

            }
        return buf.toString();
    }
    

    To answer your next question, the Java API already has a base conversion utility built in to BigInteger already. See the toString(int radix) documentation.

    Not knowing the implementation used by the Java API, I can’t say for sure, but I’d be willing to bet that the Java implenentation is more efficient than the first somewhat simple algorithm you posted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 148k
  • Answers 148k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can specify \vspace in postscript points. This will give… May 12, 2026 at 9:24 am
  • Editorial Team
    Editorial Team added an answer http://code.msdn.microsoft.com/EFOracleProvider Can you use Microsoft Entity Framework with Oracle? May 12, 2026 at 9:24 am
  • Editorial Team
    Editorial Team added an answer I wouldn't do it dynamically as the gain is not… May 12, 2026 at 9:24 am

Related Questions

I recently started looking at MD5 hashing (in Java) and while I've found algorithms
I recently switched over to a MacBook Pro so I'm still really new at
Recently I just got assigned a project to develop a web application/site that uses
I learnt HTML/CSS a good few years back, then PHP a little later. I've
Do you find that when you work with a new technology that you're never

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.