搜尋
首頁Javajava教程Java程式範例,展示十六進位的使用方法

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。如有侵權,請聯絡admin@php.cn刪除
是否有任何威脅或增強Java平台獨立性的新興技術?是否有任何威脅或增強Java平台獨立性的新興技術?Apr 24, 2025 am 12:11 AM

新興技術對Java的平台獨立性既有威脅也有增強。 1)雲計算和容器化技術如Docker增強了Java的平台獨立性,但需要優化以適應不同雲環境。 2)WebAssembly通過GraalVM編譯Java代碼,擴展了其平台獨立性,但需與其他語言競爭性能。

JVM的實現是什麼,它們都提供了相同的平台獨立性?JVM的實現是什麼,它們都提供了相同的平台獨立性?Apr 24, 2025 am 12:10 AM

不同JVM實現都能提供平台獨立性,但表現略有不同。 1.OracleHotSpot和OpenJDKJVM在平台獨立性上表現相似,但OpenJDK可能需額外配置。 2.IBMJ9JVM在特定操作系統上表現優化。 3.GraalVM支持多語言,需額外配置。 4.AzulZingJVM需特定平台調整。

平台獨立性如何降低發展成本和時間?平台獨立性如何降低發展成本和時間?Apr 24, 2025 am 12:08 AM

平台獨立性通過在多種操作系統上運行同一套代碼,降低開發成本和縮短開發時間。具體表現為:1.減少開發時間,只需維護一套代碼;2.降低維護成本,統一測試流程;3.快速迭代和團隊協作,簡化部署過程。

Java的平台獨立性如何促進代碼重用?Java的平台獨立性如何促進代碼重用?Apr 24, 2025 am 12:05 AM

Java'splatformindependencefacilitatescodereusebyallowingbytecodetorunonanyplatformwithaJVM.1)Developerscanwritecodeonceforconsistentbehavioracrossplatforms.2)Maintenanceisreducedascodedoesn'tneedrewriting.3)Librariesandframeworkscanbesharedacrossproj

您如何在Java應用程序中對平台特定問題進行故障排除?您如何在Java應用程序中對平台特定問題進行故障排除?Apr 24, 2025 am 12:04 AM

要解決Java應用程序中的平台特定問題,可以採取以下步驟:1.使用Java的System類查看系統屬性以了解運行環境。 2.利用File類或java.nio.file包處理文件路徑。 3.根據操作系統條件加載本地庫。 4.使用VisualVM或JProfiler優化跨平台性能。 5.通過Docker容器化確保測試環境與生產環境一致。 6.利用GitHubActions在多個平台上進行自動化測試。這些方法有助於有效地解決Java應用程序中的平台特定問題。

JVM中的類加載程序子系統如何促進平台獨立性?JVM中的類加載程序子系統如何促進平台獨立性?Apr 23, 2025 am 12:14 AM

類加載器通過統一的類文件格式、動態加載、雙親委派模型和平台無關的字節碼,確保Java程序在不同平台上的一致性和兼容性,實現平台獨立性。

Java編譯器會產生特定於平台的代碼嗎?解釋。Java編譯器會產生特定於平台的代碼嗎?解釋。Apr 23, 2025 am 12:09 AM

Java編譯器生成的代碼是平台無關的,但最終執行的代碼是平台特定的。 1.Java源代碼編譯成平台無關的字節碼。 2.JVM將字節碼轉換為特定平台的機器碼,確保跨平台運行但性能可能不同。

JVM如何處理不同操作系統的多線程?JVM如何處理不同操作系統的多線程?Apr 23, 2025 am 12:07 AM

多線程在現代編程中重要,因為它能提高程序的響應性和資源利用率,並處理複雜的並發任務。 JVM通過線程映射、調度機制和同步鎖機制,在不同操作系統上確保多線程的一致性和高效性。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用