首頁  >  文章  >  Java  >  Java程式範例,展示十六進位的使用方法

Java程式範例,展示十六進位的使用方法

WBOY
WBOY轉載
2023-09-10 23:37:02798瀏覽

Java程式範例,展示十六進位的使用方法

Here, the usage of Hexadecimal shall be demonstrated through the Java Program.

Let us get acquainted with the term Hexadecimal before seeing a Java program.

The Hexadecimal is a type of number system that has a base value of 16. There are 16 symbols representing hexadecimal numbers. These symbols or values are 0, 1, 2, 3, 6, 7, 8, 9, A, B, C, D, E, and F. Each digit represents a decimal value. 

十六進制數從0到9相當於十進制數從0到9。

此外,A代表10,B代表11,C代表12,D代表13,E代表14,F代表15。

Demonstrating the usage of the Hexadecimal number system through certain examples:

  • Converting Decimal numbers to Hexadecimal numbers

  • #Converting Hexadecimal numbers to Decimal numbers

  • #Converting Hexadecimal numbers to long number

  • 將長數字轉換為十六進位數字

轉化的基礎知識

Consider any decimal value and convert it into a hexadecimal number system.

Let us consider the decimal number 775 .

的中文已翻譯為:

No.

Quotient

餘數

Hex Value

#十六進位值

775/16

48

#7

7

48/16

#3

0

0

3/16

#0

3

3

因此,對應的十六進位值為307

#Let us consider another decimal number 1256.

的中文已翻譯為:

No.

Quotient

餘數

Hex Value

#十六進位值

1256/16

78

8

8

78/16

#4

14

#E

4/16

#0

4

4

因此,對應的十六進位值 = 4E8

#Example 1

的翻譯為:

範例1

#Let us see a java program to convert a decimal number into its corresponding Hexadecimal number.

// 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);
	}
}

Output

#
Hexadecimal equivalent of 4698 is 125A

This Program is written to demonstrate the working of Hexadecimal number systems. Here, a decimal number is assigned in a variable and the same is converted into a corresponding Hexadecimal number using custom logic.

##Example 2

的中文翻譯為:

範例2

Let us see a java program to convert a decimal number into its corresponding Hexadecimal number using a predefined function that is a

library function 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);
	}
}

Output

#
Hexadecimal value of 986 is 3da

這是另一個示範十六進位數使用方法的程式。在這裡,使用

toHexString 方法將十進位數轉換為對應的十六進位數。 

Example 3

的翻譯為:

範例3

Let us see a java program to convert a Hexadecimal number into its corresponding decimal number using a library function 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);
	}
}

Output

#
Corresponding Decimal value of4B6A is 19306

This Program is written to demonstrate the working of Hexadecimal number systems. Here, a Hexadecimal number is assigned to a variable and is converted into its corresponding decimal number using a library function ##intmal.

#Example 4 Let us see a java program to convert a Hexadecimal number into its corresponding long number using a library function

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);
	}
}

Output#

Corresponding Hex value of long number 2028 is 7ec
這個程式是為了示範十六進位數係統的工作原理而寫的。在這裡,一個十六進制數被賦給一個變量,並使用函式庫函數

Long.toHexString

將其轉換為對應的長整數。

Example 5 讓我們來看一個Java程序,使用函式庫函數 toHexString 將一個長整數數轉換為其對應的十六進位數。

// 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);
    }
}

Output

#
Corresponding Long value of hexadecimal no. 7850 is 30800

This Program is written to demonstrate the working of Hexadecimal number systems. Here, a hexadecimal number is assigned to a variable and is converted into its corresponding long number using a library function ##Long#.

本文解釋了在Java中使用十六進位的用法。我們展示了四種使用十六進制值的方法。我們也看到了五種不同的實作方式,以了解其用法。

以上是Java程式範例,展示十六進位的使用方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除