Java程式示例:十六進位制的使用


這裡,我們將透過Java程式演示十六進位制的使用。

在檢視Java程式之前,讓我們先了解一下十六進位制的概念。

十六進位制是一種以16為基數的數字系統。它有16個符號來表示十六進位制數。這些符號或值是0、1、2、3、4、5、6、7、8、9、A、B、C、D、E和F。每個數字代表一個十進位制值。

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

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

透過一些例子演示十六進位制數系統的使用

  • 將十進位制數轉換為十六進位制數

  • 將十六進位制數轉換為十進位制數

  • 將十六進位制數轉換為長整數

  • 將長整數轉換為十六進位制數

轉換基礎

考慮任何十進位制值並將其轉換為十六進位制數系統。

讓我們考慮十進位制數775

序號

餘數

十六進位制值

775/16

48

7

7

48/16

3

0

0

3/16

0

3

3

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

讓我們考慮另一個十進位制數1256

序號

餘數

十六進位制值

1256/16

78

8

8

78/16

4

14

E

4/16

0

4

4

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

示例1

讓我們來看一個Java程式,將十進位制數轉換為其對應的十六進位制數。

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

輸出

Hexadecimal equivalent of 4698 is 125A

此程式旨在演示十六進位制數系統的執行方式。這裡,一個十進位制數被賦給一個變數,並使用自定義邏輯將其轉換為對應的十六進位制數。

示例2

讓我們來看一個Java程式,使用預定義函式(即庫函式)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);
	}
}

輸出

Hexadecimal value of 986 is 3da

這是另一個演示十六進位制數使用的程式。這裡,使用toHexString方法將十進位制數轉換為其對應的十六進位制數。

示例3

讓我們來看一個Java程式,使用庫函式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);
	}
}

輸出

Corresponding Decimal value of4B6A is 19306

此程式旨在演示十六進位制數系統的執行方式。這裡,一個十六進位制數被賦給一個變數,並使用庫函式Integer.parseInt將其轉換為其對應的十進位制數。

示例4

讓我們來看一個Java程式,使用庫函式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);
	}
}

輸出

Corresponding Hex value of long number 2028 is 7ec

此程式旨在演示十六進位制數系統的執行方式。這裡,一個十六進位制數被賦給一個變數,並使用庫函式Long.toHexString將其轉換為其對應的長整數。

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

輸出

Corresponding Long value of hexadecimal no. 7850 is 30800

此程式旨在演示十六進位制數系統的執行方式。這裡,一個十六進位制數被賦給一個變數,並使用庫函式Long.parseLong將其轉換為其對應的長整數。

本文闡述了在Java中使用十六進位制的方法。我們演示了十六進位制值的四種用法,並透過五個不同的實現來理解其用法。

更新於:2023年8月11日

196 次瀏覽

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.