Java >> Tutoriel Java >  >> Tag >> class

Méthode Java Integer class toHexString() avec exemple

Méthode classe entière versHexString()

  • méthode toHexString() est disponible dans le package java.lang.
  • méthode toHexString() est utilisé pour représenter une chaîne hexadécimale du paramètre donné [valeur] de type entier sous la forme d'un entier non signé en base 16.
  • méthode toHexString() est une méthode statique, elle est également accessible avec le nom de la classe et si nous essayons d'accéder à la méthode avec l'objet de la classe, nous n'obtiendrons pas non plus d'erreur.
  • méthode toHexString() ne lève pas d'exception au moment de la conversion d'entier en chaîne hexadécimale.

Syntaxe :

    public static String toHexString (int value);

Paramètre(s) :

  • int value – représente la valeur entière à convertir.

Valeur renvoyée :

Le type de retour de cette méthode est int, elle retourne la chaîne hexadécimale du paramètre donné qui représente la valeur entière non signée.

Exemple :

// Java program to demonstrate the example 
// of toHexString (int value) method of Integer class

public class ToHexStringOfIntegerClass {
    public static void main(String[] args) {
        // Variables initialization
        int i1 = 10;
        int i2 = 20;
        int i3 = 30;
        int i4 = Integer.MAX_VALUE;
        int i5 = Integer.MIN_VALUE;

        // Integer instance creation
        Integer value = new Integer(i1);

        // It represents hexadecimal string of the given
        // integer type i2 argument
        String s = value.toHexString(i2);

        // Display Hexadecimal String Representation
        System.out.println("value.toHexString(i2): " + s);

        // It represents hexadecimal string of the given
        // integer type i3 argument
        s = value.toHexString(i3);

        // Display Hexadecimal String Representation
        System.out.println("value.toHexString(i3): " + s);

        // It represents hexadecimal string of the given
        // integer type i4 argument
        s = value.toHexString(i4);

        // Display Hexadecimal String Representation
        System.out.println("value.toHexString(i4): " + s);

        // It represents hexadecimal string of the given
        // integer type i5 argument
        s = value.toHexString(i5);

        // Display Hexadecimal String Representation
        System.out.println("value.toHexString(i5): " + s);
    }
}

Sortie

value.toHexString(i2): 14
value.toHexString(i3): 1e
value.toHexString(i4): 7fffffff
value.toHexString(i5): 80000000

Balise Java