在Java中将十六进制转换为八进制?

2023年 8月 28日 78.9k 0

在Java中将十六进制转换为八进制?

Octal Number − Octal number is also one of the number systems available. The octal number is represented with 8 digits which is from 0 to 7(0, 1, 2, 3... 7). The Octal numbers are expressed as base-8 in the numeral system.

Hexadecimal Number − Hexadecimal number is also one of the number systems available. The Hexadecimal number is represented with 16 digits which is from 0 to 15(0, 1, 2, 3... 15). From 10 to 15 it is represented as A to F. The Hexadecimal numbers are expressed as base-16 in the numeral system.

Here we first convert the hexadecimal numbers into binary numbers, where we get the binary numbers combination of four digits for each digit. After getting all those binary digits we concatenate all those digits then we have to divide the whole set of binary numbers into chucks where every part consists of three digits. Then we can convert those sets of binary numbers into octal numbers. By this way we convert the Hexadecimal number into octal number.

In other way after getting the decimal value we continuously find the modulus by 8 values and then by concatenating those values we can get the appropriate octal value.

让我们看看如何使用Java编程语言来完成这个任务。

To show you some instances

Instance-1

的中文翻译为:

实例-1

Input Hexadecimal number is 9AD

The decimal converted value of it = 2477

Now the octal value of 2477 = 4655

Instance-2

的中文翻译为:

实例-2

Input Hexadecimal number is 219A

The decimal converted value of it = 8602

现在8602的八进制值为20632

Instance-3

的中文翻译为:

实例-3

Input Hexadecimal number is 21AD45

它的十进制转换值 = 2207045

现在2207045的八进制值为10326505

算法

Step 1 − Get the input number as string type either by static input method or user input method.

Step 2 − Using some switch cases we define the appropriate decimal value of each digit of the given hexadecimal number.

第三步 - 在获得十进制值后,我们通过不断地找到8的模数值并连接它们来将其转换为适当的八进制值。

Step 4 − At the end we print the calculated Octal value as output.

Multiple Approaches

We have provided the solution in different approaches.

  • 通过使用静态输入值

  • 通过用户定义的方法

让我们逐个查看程序及其输出。

途径-1:通过使用静态输入值

在这种方法中,我们通过静态输入方法声明一个十六进制输入数字,通过使用算法,我们可以将十六进制数字转换为八进制数字。

Example

public class Main{
public static void main(String[] args){

//declare a variable to store the decimal number
int decimalNumber = 0;

//Declare and store a hexadecimal number by static input method.
String hexadecimalNumber = "87FA";
int a = hexadecimalNumber.length() - 1;

//Loop to find the appropriate decimal number of given hexadecimal number
for(int i = 0; i 0){
octalNumber = decimalNumber % 8 + octalNumber;
decimalNumber = decimalNumber / 8;
}
System.out.println("The Octal Value of "+ hexadecimalNumber + " is " + octalNumber + ".");
}
}

登录后复制

Output

The Octal Value of 87FA is 103772.

登录后复制

Approach-2: By Using User Defined Method

In this approach, we declare a hexadecimal input number by static input method and pass these numbers as parameters in a user defined method, then inside the method by using the algorithm we can convert the hexadecimal number into octal number.

Example

public class Main{
public static void main(String[] args){
String inputNumber = "6FE4";//Declare and store a hexadecimal number by static input method.
hexToOct(inputNumber);//call the user defined method to convert given hexadecimal number into octal.
}
//user defined method to convert the hexadecimal number into octal number
public static void hexToOct(String hexadecimalNumber){
int decimalNumber = 0;//declare a variable to store the decimal number
int a = hexadecimalNumber.length() - 1;
//Loop to find the appropriate decimal number of given hexadecimal number
for(int i = 0; i 0){
octalNumber = decimalNumber % 8 + octalNumber;
decimalNumber = decimalNumber / 8;
}
System.out.println("The Octal Value of "+ hexadecimalNumber + " is " + octalNumber + ".");
}
}

登录后复制

Output

The Octal Value of 6FE4 is 67744.

登录后复制

In this article, we explored how to convert a hexadecimal number to octal number in Java by using different approaches.

以上就是在Java中将十六进制转换为八进制?的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论