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 gRPC 服务开发与Protobuf实战
Golang gRPC 服务开发与Protobuf实战

本专题系统讲解 Golang 在 gRPC 服务开发中的完整实践,涵盖 Protobuf 定义与代码生成、gRPC 服务端与客户端实现、流式 RPC(Unary/Server/Client/Bidirectional)、错误处理、拦截器、中间件以及与 HTTP/REST 的对接方案。通过实际案例,帮助学习者掌握 使用 Go 构建高性能、强类型、可扩展的 RPC 服务体系,适用于微服务与内部系统通信场景。

8

2026.01.15

公务员递补名单公布时间 公务员递补要求
公务员递补名单公布时间 公务员递补要求

公务员递补名单公布时间不固定,通常在面试前,由招录单位(如国家知识产权局、海关等)发布,依据是原入围考生放弃资格,会按笔试成绩从高到低递补,递补考生需按公告要求限时确认并提交材料,及时参加面试/体检等后续环节。要求核心是按招录单位公告及时响应、提交材料(确认书、资格复审材料)并准时参加面试。

44

2026.01.15

公务员调剂条件 2026调剂公告时间
公务员调剂条件 2026调剂公告时间

(一)符合拟调剂职位所要求的资格条件。 (二)公共科目笔试成绩同时达到拟调剂职位和原报考职位的合格分数线,且考试类别相同。 拟调剂职位设置了专业科目笔试条件的,专业科目笔试成绩还须同时达到合格分数线,且考试类别相同。 (三)未进入原报考职位面试人员名单。

58

2026.01.15

国考成绩查询入口 国考分数公布时间2026
国考成绩查询入口 国考分数公布时间2026

笔试成绩查询入口已开通,考生可登录国家公务员局中央机关及其直属机构2026年度考试录用公务员专题网站http://bm.scs.gov.cn/pp/gkweb/core/web/ui/business/examResult/written_result.html,查询笔试成绩和合格分数线,点击“笔试成绩查询”按钮,凭借身份证及准考证进行查询。

11

2026.01.15

Java 桌面应用开发(JavaFX 实战)
Java 桌面应用开发(JavaFX 实战)

本专题系统讲解 Java 在桌面应用开发领域的实战应用,重点围绕 JavaFX 框架,涵盖界面布局、控件使用、事件处理、FXML、样式美化(CSS)、多线程与UI响应优化,以及桌面应用的打包与发布。通过完整示例项目,帮助学习者掌握 使用 Java 构建现代化、跨平台桌面应用程序的核心能力。

65

2026.01.14

php与html混编教程大全
php与html混编教程大全

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

36

2026.01.13

PHP 高性能
PHP 高性能

本专题整合了PHP高性能相关教程大全,阅读专题下面的文章了解更多详细内容。

75

2026.01.13

MySQL数据库报错常见问题及解决方法大全
MySQL数据库报错常见问题及解决方法大全

本专题整合了MySQL数据库报错常见问题及解决方法,阅读专题下面的文章了解更多详细内容。

21

2026.01.13

PHP 文件上传
PHP 文件上传

本专题整合了PHP实现文件上传相关教程,阅读专题下面的文章了解更多详细内容。

35

2026.01.13

热门下载

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

精品课程

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

共23课时 | 2.5万人学习

C# 教程
C# 教程

共94课时 | 6.8万人学习

Java 教程
Java 教程

共578课时 | 46.2万人学习

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

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