In Java, how do I convert a byte array to a string of hex digits while keeping leading zeros?

Eugene M :

I'm working with some example java code for making md5 hashes. One part converts the results from bytes to a string of hex digits:

byte messageDigest[] = algorithm.digest();     
StringBuffer hexString = new StringBuffer();
for (int i=0;i<messageDigest.length;i++) {
    hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
    }

However, it doesn't quite work since toHexString apparently drops off leading zeros. So, what's the simplest way to go from byte array to hex string that maintains the leading zeros?

Michael Myers :

A simple approach would be to check how many digits are output by Integer.toHexString() and add a leading zero to each byte if needed. Something like this:

public static String toHexString(byte[] bytes) {
    StringBuilder hexString = new StringBuilder();

    for (int i = 0; i < bytes.length; i++) {
        String hex = Integer.toHexString(0xFF & bytes[i]);
        if (hex.length() == 1) {
            hexString.append('0');
        }
        hexString.append(hex);
    }

    return hexString.toString();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to convert an int to hex with leading zeros in Java?

In Java, how do I convert a hex string to a byte[]?

How to convert a byte array to a hex string in Java?

How to increment the number in a string while keeping the appropriate trailing/leading zeros

How can I convert a hex string to a byte array?

How do i convert string.byte to their ascii hex value

How do I convert separate int values to hex byte array

How to convert a byte array to hex string?

How do I convert a Java byte array into a Scala byte array?

Python-Turning a string into and integer while keeping leading zeros

Convert byte array to hex string

Convert hex string to byte array

How to convert an integer to string with leading zeros in C?

String.format() rounding a double with leading zeros in digits - Java

What format is this, and how do I convert a byte array into this string format?

Java generic convert byte array to string (Non hex, decimal)

Convert java string with odd length to hex byte array

Java: Convert HEX byte array to 16 fixed length binary String

Convert a string representation of a hex dump to a byte array using Java?

How do I convert a byte array with null terminating character to a String in Java?

How to convert string of hex digits to string wtih hexdecimal byte escapes in hbase shell (JRuby)

How does a person go about learning Java? (convert byte array to hex string)

How do i create byte array that contains 64 bits array and how do i convert those bits into hex value?

How to convert a normal string to an equivalent byte array in hex?

How do I convert a string into HEX representation?

How do I convert a string to hex in Rust?

How to get number of digits in an integer with leading zeros in java?

How can I convert an integer to a hex byte stored in a char array?

How do I display a byte array as an array of hex bytes or unsigned decimal numbers in the Eclipse Java debugger?