Java程式:將八進位制轉換為十六進位制
八進位制數
八進位制數也是一種可用的數字系統。八進位制數用8個數字表示,從0到7(0, 1, 2, 3... 7)。八進位制數在數字系統中表示為基數-8。
十六進位制數
十六進位制數也是一種可用的數字系統。十六進位制數用16個數字表示,從0到15(0, 1, 2, 3... 15)。從10到15用A到F表示。十六進位制數在數字系統中表示為基數-16。
問題陳述
首先將八進位制數轉換為其相應的十進位制數,然後再次將該十進位制數轉換為十六進位制數。透過這種方式,我們將八進位制數轉換為十六進位制數。
在這篇文章中,我們將瞭解如何在Java中將八進位制轉換為十六進位制。
展示一些例項:
例項-1
Input octal number is 1232. The decimal converted value of it = 666. Now the hexadecimal value of 666 = 29A.
例項-2
Input octal number is 5454. The decimal converted value of it = 2860. Now the hexadecimal value of 2860= B2C.
例項-3
Input octal number is 76564. The decimal converted value of it = 32116. Now the hexadecimal value of 32116 = 7D74.
演算法
步驟1 透過靜態輸入或使用者輸入方法獲取輸入數字。
步驟2 我們首先使用Integer.parseInt(octalNumber, 8)方法將給定的八進位制數轉換為十進位制數。
步驟3 然後,我們使用Integer.toHexString(decimalNumber)方法將十進位制數轉換為十六進位制數。
步驟4 獲取十六進位制值後,我們將該值作為輸出列印。
多種方法
我們提供了多種方法的解決方案。
- 使用使用者自定義方法和靜態輸入值。
- 使用使用者自定義方法和使用者輸入值。
讓我們一一檢視程式及其輸出。
方法-1:使用使用者自定義方法和靜態輸入值
在此方法中,我們將八進位制輸入數字宣告為靜態輸入,並將此數字作為引數傳遞給使用者自定義方法,然後在方法內部,使用演算法將八進位制數轉換為二進位制數。
示例
public class Main {
public static void main(String args[]){
//declare and store a octal number by static input method
String inputNumber = "7654";
//call the user defined method to give the output
octalToHex(inputNumber);
}
//user defined method to convert the octal number into hexadecimal number
public static void octalToHex(String octalNumber){
//declare a variable
//Convert the given Octal number to Decimal number and store it into that variable
int decimalNumber = Integer.parseInt(octalNumber, 8);
//declare a variable
//now convert Decimal number to Hexadecimal number and store it into that variable
String hexadecimalNumber = Integer.toHexString(decimalNumber);
System.out.print("The Hexadecimal Value of "+ octalNumber + " is " + hexadecimalNumber);
}
}
輸出
The Hexadecimal Value of 7654 is fac
方法-2:使用使用者自定義方法和使用者輸入值。
在此方法中,我們透過使用者輸入宣告八進位制輸入數字,並將這些數字作為引數傳遞給使用者自定義方法,然後在方法內部,使用演算法將八進位制數轉換為十六進位制數。
示例
import java.util.Scanner;
public class Main {
public static void main(String args[]){
//create object of scanner class
Scanner sc = new Scanner(System.in);
//ask the user to enter an octal number
System.out.print("Enter an Octal Number = ");
//declare and store an octal number by user input method
String inputNumber = sc.nextLine();
//call the user defined method to give the output
octalToHex(inputNumber);
}
//user defined method to convert the octal number into hexadecimal number
public static void octalToHex(String octalNumber) {
//declare a variable
// Convert the given Octal number to Decimal number and store it into that variable
int decimalNumber = Integer.parseInt(octalNumber, 8);
//declare a variable
// now convert Decimal number to Hexadecimal number and store it into that variable
String hexadecimalNumber = Integer.toHexString(decimalNumber);
System.out.print("The Hexadecimal Value of "+ octalNumber + " is " + hexadecimalNumber);
}
}
輸出
Enter an Octal Number = 45342 The Hexadecimal Value of 45342 is 4ae2
在這篇文章中,我們探討了如何使用不同的方法在Java中將八進位制數轉換為十六進位制數。
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP