0

0

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

WBOY

WBOY

发布时间:2023-08-19 13:37:05

|

1482人浏览过

|

来源于tutorialspoint

转载

在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免费学习笔记(深入)”;

让我们看看如何使用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

猫宁Morning公益商城系统
猫宁Morning公益商城系统

猫宁Morning公益商城是中国公益性在线电子商城,以商城B2C模式运营的公益在线商城,是一家致力于将传统公益商城互联网化的创新公益商城。该网上商城系统分为电子商城系统、公益商城系统、后台管理系统,使用Maven对项目进行模块化管理,搭建多模块企业级项目。Morning是在Spring Framework基础上搭建的一个Java基础开发平台,以Spring MVC为模型视图控制器,MyBatis为

下载

它的十进制转换值 = 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 < hexadecimalNumber.length() ; i ++ ){
         
         //extract the character from the string.
         char c = hexadecimalNumber.charAt(i);
         switch (c){
            case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8':
            case '9':
               decimalNumber = decimalNumber + Integer.parseInt(Character.toString(c))*(int)Math.pow(16,a);
               a--; break;
            case 'a': case 'A':
               decimalNumber = decimalNumber + 10 * (int)Math.pow(16, a);
               a--; break;
            case 'b': case 'B':
               decimalNumber = decimalNumber + 11 * (int)Math.pow(16, a);
               a--; break;
            case 'c': case 'C':
               decimalNumber = decimalNumber + 12 * (int)Math.pow(16, a);
               a--; break;
            case 'd': case 'D':
               decimalNumber = decimalNumber + 13 * (int)Math.pow(16, a);
               a--;
               break;
            case 'e': case 'E':
               decimalNumber = decimalNumber + 14 * (int)Math.pow(16, a);
                a--; break;
            case 'f': case 'F':
               decimalNumber = decimalNumber + 15 * (int)Math.pow(16, a);
               a--; break;
            default:
            System.out.println("The number you have entered is invalid."); break;
         }
      }
      String octalNumber ="";// declare a variable to store the octal number in string format.
      
      //initiate the loop to convert decimal number into octal number.
      while(decimalNumber > 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 < hexadecimalNumber.length() ; i ++ ){
         //extract the character from the string.
         char c = hexadecimalNumber.charAt(i);
         switch (c){
            case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8':
            case '9':
               decimalNumber = decimalNumber + Integer.parseInt(Character.toString(c))*(int)Math.pow(16,a);
               a--; break;
            case 'a': case 'A':
               decimalNumber = decimalNumber + 10 * (int)Math.pow(16, a);
               a--; break;
            case 'b': case 'B':
               decimalNumber = decimalNumber + 11 * (int)Math.pow(16, a);
               a--; break;
            case 'c':
               decimalNumber = decimalNumber + 12 * (int)Math.pow(16, a);
               a--; break;
            case 'd': case 'D':
               decimalNumber = decimalNumber + 13 * (int)Math.pow(16, a);
               a--; break;
            case 'e': case 'E':
               decimalNumber = decimalNumber + 14 * (int)Math.pow(16, a);
               a--; break;
            case 'f': case 'F':
               decimalNumber = decimalNumber + 15 * (int)Math.pow(16, a);
               a--; break;
            default:
               System.out.println("The number you have entered is invalid."); break;
         }
      }
      String octalNumber ="";// declare a variable to store the octal number in string format.
      //initiate the loop to convert decimal number into octal number.
      while(decimalNumber > 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速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
Golang 性能分析与pprof调优实战
Golang 性能分析与pprof调优实战

本专题系统讲解 Golang 应用的性能分析与调优方法,重点覆盖 pprof 的使用方式,包括 CPU、内存、阻塞与 goroutine 分析,火焰图解读,常见性能瓶颈定位思路,以及在真实项目中进行针对性优化的实践技巧。通过案例讲解,帮助开发者掌握 用数据驱动的方式持续提升 Go 程序性能与稳定性。

9

2026.01.22

html编辑相关教程合集
html编辑相关教程合集

本专题整合了html编辑相关教程合集,阅读专题下面的文章了解更多详细内容。

53

2026.01.21

三角洲入口地址合集
三角洲入口地址合集

本专题整合了三角洲入口地址合集,阅读专题下面的文章了解更多详细内容。

28

2026.01.21

AO3中文版入口地址大全
AO3中文版入口地址大全

本专题整合了AO3中文版入口地址大全,阅读专题下面的的文章了解更多详细内容。

364

2026.01.21

妖精漫画入口地址合集
妖精漫画入口地址合集

本专题整合了妖精漫画入口地址合集,阅读专题下面的文章了解更多详细内容。

111

2026.01.21

java版本选择建议
java版本选择建议

本专题整合了java版本相关合集,阅读专题下面的文章了解更多详细内容。

3

2026.01.21

Java编译相关教程合集
Java编译相关教程合集

本专题整合了Java编译相关教程,阅读专题下面的文章了解更多详细内容。

15

2026.01.21

C++多线程相关合集
C++多线程相关合集

本专题整合了C++多线程相关教程,阅读专题下面的的文章了解更多详细内容。

9

2026.01.21

无人机驾驶证报考 uom民用无人机综合管理平台官网
无人机驾驶证报考 uom民用无人机综合管理平台官网

无人机驾驶证(CAAC执照)报考需年满16周岁,初中以上学历,身体健康(矫正视力1.0以上,无严重疾病),且无犯罪记录。个人需通过民航局授权的训练机构报名,经理论(法规、原理)、模拟飞行、实操(GPS/姿态模式)及地面站训练后考试合格,通常15-25天拿证。

45

2026.01.21

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Kotlin 教程
Kotlin 教程

共23课时 | 2.8万人学习

C# 教程
C# 教程

共94课时 | 7.3万人学习

Java 教程
Java 教程

共578课时 | 49.3万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号