Maison  >  Article  >  Java  >  Exemple de programme Java montrant comment utiliser l'hexadécimal

Exemple de programme Java montrant comment utiliser l'hexadécimal

WBOY
WBOYavant
2023-09-10 23:37:02798parcourir

Exemple de programme Java montrant comment utiliser lhexadécimal

Ici, l'utilisation de l'hexadécimal doit être démontrée via le programme Java.

Faisons connaissance avec le terme Hexadécimal avant de voir un programme Java.

Le Hexadécimal est un type de système numérique qui a une valeur de base de 16. Il existe 16 symboles représentant des nombres hexadécimaux. Ces symboles ou valeurs sont 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E et F. Chaque chiffre représente une valeur décimale

.

Les nombres hexadécimaux de 0 à 9 sont équivalents aux nombres décimaux de 0 à 9.

De plus, A représente 10, B représente 11, C représente 12, D représente 13, E représente 14 et F représente 15.

Démontrer l'utilisation du système de nombres hexadécimaux à travers certains exemples :

  • Conversion de nombres décimaux en nombres hexadécimaux

  • Conversion de nombres hexadécimaux en nombres décimaux

  • Conversion de nombres hexadécimaux en nombres longs

  • Convertir des nombres longs en nombres hexadécimaux

Connaissance de base de la conversion

Considérez n'importe quelle valeur décimale et convertissez-la en un système numérique hexadécimal.

Considérons le nombre décimal 775 .

La traduction chinoise de est :

Non.

Quotient

Reste

Valeur hexadécimale

Valeur hexadécimale

775/16

48

7

7

48/16

3

0

0

3/16

0

3

3

Par conséquent, la valeur hexadécimale correspondante est 307

Considérons un autre nombre décimal 1256.

La traduction chinoise de est :

Non.

Quotient

Reste

Valeur hexadécimale

Valeur hexadécimale

1256/16

78

8

8

78/16

4

14

E

4/16

0

4

4

Par conséquent, la valeur hexadécimale correspondante = 4E8

Exemple 1

se traduit par :

Exemple 1

Voyons un programme Java pour convertir un nombre décimal en son nombre hexadécimal correspondant.

// Java program to convert a decimal
// number into a hexadecimal number
import java.io.*;
public class DectoHexadecimal {
	// method to convert decimal to hexadecimal number system
	static void decToHexa(int num)	{
		// char array that stores the hexadecimal number
		char[] hexaDec = new char[100];
		int j = 0;
		while (num != 0) {
			// temporary variable to store the remainder
			int t = 0;
			// storing the remainder in the temp variable.
			t = num % 16;
			// check if temp < 10
			if (t < 10) {
				hexaDec[j] = (char)(t + 48);
				j++;
			} else {
				hexaDec[j] = (char)(t + 55);
				j++;
			}

			num = num / 16;
		}
		// hexadecimal number array in reverse order
	
		for (int k = j - 1; k >= 0; k--)
			System.out.print( hexaDec[k]);
	}
	public static void main(String[] args){
		int num = 4698;
		System.out.print("Hexadecimal equivalent of " +num+ " is " );
		decToHexa(num);
	}
}

Sortie

Hexadecimal equivalent of 4698 is 125A

Ce programme est écrit pour démontrer le fonctionnement des systèmes de nombres hexadécimaux. Ici, un nombre décimal est attribué à une variable et celui-ci est converti en un nombre hexadécimal correspondant à l'aide d'une logique personnalisée.

La traduction chinoise de

Exemple 2

est :

Exemple 2

Voyons un programme Java pour convertir un nombre décimal en son nombre hexadécimal correspondant à l'aide d'une fonction prédéfinie qui est une fonction de bibliothèque toHexString.

// Java Program to demonstrate the Usage of HexaDecimal
import java.io.*;
public class DecimaltoHex {
	// Main  method
	public static void main(String[] args){
		//Decimal number to be converted
		int d = 986;
		// Using the toHexString() method to convert decimal value to its 
		// corresponding hexadecimal value
		String hexd = Integer.toHexString(d);
		// Displaying hexaDec value
		System.out.println("Hexadecimal value of " +d+ " is " + hexd);
	}
}

Sortie

Hexadecimal value of 986 is 3da

Voici un autre programme qui démontre l'utilisation des nombres hexadécimaux. Ici, la méthode toHexString est utilisée pour convertir le nombre décimal en nombre hexadécimal correspondant.

Exemple 3

se traduit par :

Exemple 3

Voyons un programme Java pour convertir un nombre hexadécimal en son nombre décimal correspondant à l'aide d'une fonction de bibliothèque parseInt.

// Java program to convert Hexadecimal numbers to corresponding Decimal number
import java.io.*;
public class HexToDecimal {
	// Main method
	public static void main(String[] args)	{
		// Random Hexadecimal number stored in a string
		String hexd = "4B6A";
		// Passing hexd and base 16 as parameters
		// to parseInt method
		int dec = Integer.parseInt(hexd, 16);
		// Displaying the corresponding
		// decimal value of a hexadecimal number
		System.out.println("Corresponding Decimal value of" + hexd + " is " + dec);
	}
}

Sortie

Corresponding Decimal value of4B6A is 19306

Ce programme est écrit pour démontrer le fonctionnement des systèmes de nombres hexadécimaux. Ici, un nombre hexadécimal est attribué à une variable et est converti en son nombre décimal correspondant à l'aide d'une fonction de bibliothèque integer.parseInt .

Exemple 4

Voyons un programme Java pour convertir un nombre hexadécimal en son nombre long correspondant à l'aide d'une fonction de bibliothèque Long.toHexString.

// Java Program to demonstrate the Usage of HexaDecimal
import java.io.*;
public class LongToHex {
	// Main method
	public static void main(String[] args)	{
		// Long number to be converted 
		long l = 2028;
		// Storing the result in a string hexd
		String hexd = Long.toHexString(l);
		// Displaying the corresponding hexadecimal value
		System.out.println("Corresponding Hex value of long number " +l+ " is " + hexd);
	}
}

Sortie

Corresponding Hex value of long number 2028 is 7ec

Ce programme a été écrit pour démontrer le fonctionnement du système de nombres hexadécimaux. Ici, un nombre hexadécimal est attribué à une variable et converti en entier long correspondant à l'aide de la fonction de bibliothèque Long.toHexString.

Exemple 5

Regardons un programme Java qui utilise la fonction de bibliothèque toHexString pour convertir un entier long en son nombre hexadécimal correspondant.

// Java Program to Illustrate the Usage of HexaDecimal by converting a hexadecimal value into long value
import java.io.*;
public class HexToLong {
    // Main method
    public static void main(String[] args) {
        // Hexadecimal number to be converted
        String hs = "7850";
        // passing hs and base 16 as parameters
        //  to parseLong function
        long l = Long.parseLong(hs, 16);
        // Displaying the corresponding hexadecimal value
        System.out.println("Corresponding Long value of hexadecimal no. " +hs+ " is " + l);
    }
}

Sortie

Corresponding Long value of hexadecimal no. 7850 is 30800

Ce programme est écrit pour démontrer le fonctionnement des systèmes de nombres hexadécimaux. Ici, un nombre hexadécimal est attribué à une variable et est converti en son nombre long correspondant à l'aide d'une fonction de bibliothèque Long.parseLong

.

Cet article explique l'utilisation de l'hexadécimal en Java. Nous montrons quatre façons d’utiliser les valeurs hexadécimales. Nous avons également vu cinq implémentations différentes pour comprendre son utilisation.

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Cet article est reproduit dans:. en cas de violation, veuillez contacter admin@php.cn Supprimer