Questions about formatting hexadecimal numbers in java for use in MD5

4

I'd like to know why the article's author has put this part:

senha = String.format("%1$032X", i); 

I was curious how he got this string: "%1$032X"

Follow the complete code:

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 {

    public static void main(String[] args)
    {

        String senha = "photu5678";
        MessageDigest mensagem;

        try
        {
            mensagem = MessageDigest.getInstance("MD5");
            mensagem.update(senha.getBytes(), 0, senha.length());
            BigInteger i = new BigInteger(1, mensagem.digest());

            // Formatando o resultado em uma cadeia de 32 caracteres, completando com 0 caso falte 
            senha = String.format("%1$032X", i); 

            System.out.println("MD5: " + senha); 
        } 

        catch (NoSuchAlgorithmException e) 
        { 
            e.printStackTrace(); 
        } 
    }
} 
    
asked by anonymous 13.03.2016 / 16:50

1 answer

4

The answer is in the Formatter class that is used by the String.format :

  
  • The format specifiers for general, character, and numeric types have the following syntax:

    %[argument_index$][flags][width][.precision]conversion
    
         The optional argument_index is a decimal integer indicating the position of the argument in the argument list. The first argument is referenced by " 1$ ", the second by " 2$ ", etc.

         

    The optional flags is a set of characters that modify the output format. The set of valid flags depends on the conversion.

         The optional width is a positive decimal integer indicating the minimum number of characters to be written to the output.

         The optional precision is a non-negative decimal integer usually used to restrict the number of characters. The specific behavior depends on the conversion.

         The required conversion is a character indicating how the argument should be formatted. The set of valid conversions for a given argument depends on the argument's data type.

  •   

Translating to Portuguese:

  
  • Format specifiers for general, character, and numeric types have the following syntax:

    %[argument_index$][flags][width][.precision]conversion
    
         

    The optional argument_index is a decimal integer indicating the position of the argument in the argument list. The first argument is referenced by " 1$ ", the second by " 2$ ", etc.

         

    The optional flags is a character set that modifies the output format. The set of valid flags depends on conversion .

         

    The optional width is a positive decimal integer indicating the minimum number of characters to be written to the output.

         

    The optional precision is a non-negative decimal integer usually used to restrict the number of characters. The specific behavior depends on conversion .

         

    The required conversion is the character indicating how the argument will be formatted. The set of valid conversions for a given argument depends on the data type of the argument.

  •   

In the case of conversion , this is what the Formatter class specifies, among other things:

  

'x' , 'X' The result is formatted as a hexadecimal integer

Translating:

  

'x' , 'X' The result is formatted as a hexadecimal integer

According to the documentation, the conversions uppercase will produce the output with uppercase and lowercase letters.

As for flags :

  

'0' The result will be zero-padded

Translating:

  

'0' The result will be completed with zeros [on the left]

That is, in the case of "%1$032X" , we have the following:

  • % indicates that the text that follows is a format specifier.
  • 1$ is the argument_index that references the first argument of the argument list (it will be the contents of the i variable).
  • 0 is flags which specifies that the number must be completed with leading zeros.
  • 32 is the width , that is, the length of the number. That is, 32 characters.
  • X is conversion , which specifies that the number will be expressed in hexadecimal digits, using uppercase letters for the A-F digits.
13.03.2016 / 17:42