Java 演示八进制整数的用法

Java 演示八进制整数的用法

八进制整数 是一个以8为基数、数字范围为0到7的数制。Int数据类型被用来存储八进制数。

这里将讨论八进制数的用法:

  • 将十进制转换为八进制。

  • 将八进制转换为十进制。

从十进制数制转换为八进制数制

转换基础知识

  • 将任意十进制数转换为相应的八进制数:

  • 将十进制数除以八(八进制数制的基数)直到商为0。

  • 记住每一步的余数。

  • 最后,按相反的顺序写下余数,所得数即为对应的八进制数。

  • 让我们通过一些示例更好地理解这个概念。

    示例1 - 考虑一个十进制数2894,并找出其八进制等价物。

Decimal     Division        Quotient        Remainder

2894        2894 / 8        461         6
461     461 / 8     57          5
57      57 / 8      7           1
7       7 / 8       0           7

因此, 7156 是十进制数 2894 的八进制等价数。

示例2 - 考虑另一个十进制数 101 并找出它的八进制等价数。

Decimal         Division    Quotient        Remainder

101     101 / 8     12              5
12      12 / 8      1               4
1       1 / 8       0               1

因此, 145 是十进制数 101 的八进制等价数。

示例

以下程序使用自定义逻辑将十进制数转换为相应的八进制数。在名为 DectoOctal 的类中创建了一个名为 DecimalToOctal1 的方法,用于演示八进制数系统的工作原理。在这里,一个十进制数 15 作为实际参数传递给创建的方法,并使用 自定义逻辑 将其转换为相应的八进制数。

//Java Program to illustrate the decimal to octal conversion
//using custom logic
public class DecimalToOctal1 {

   //creating a method to convert decimal numbers into octal numbers
   public static String DectoOctal(int dec) {
      int r;
      String octal="";

      //declaring and initializing an array octalchr
      char octalchr[]= {'0', '1', '2', '3', '4', '5', '6', '7'};

      //logic for conversion
      while(dec>0) {
         r=dec%8;
         octal=octalchr[r]+octal;
         dec=dec/8;
      }
      return octal;
   }
   public static void main(String args[]) {

      //Invoking the above method to get the octal number of the below decimal numbers
      System.out.println("Octal equivalent of decimal number 15 is: "+DectoOctal(15));
      System.out.println("Octal equivalent of decimal number 24 is: "+DectoOctal(24));
      System.out.println("Octal equivalent of decimal number 7 is: "+DectoOctal(7));
      System.out.println("Octal equivalent of decimal number 5 is: "+DectoOctal(5));
   }
}

输出

Octal equivalent of decimal number 15 is: 17
Octal equivalent of decimal number 24 is: 30
Octal equivalent of decimal number 7 is: 7
Octal equivalent of decimal number 5 is: 5

示例

以下程序使用库函数将一个十进制数转换为其相应的八进制数。在名为DecimalToOctal2的类中,使用库函数Integer.toOctalString()将四个不同的十进制数转换为它们相应的八进制值。

//Java Program to demonstrate the conversion of decimal number into its equivalent octal number using a library function
public class DecimalToOctal2 {
   public static void main(String args[]) {
      //By the usage of the Integer.toOctalString() library method
      //to convert a decimal value into a corresponding octal number

      System.out.println("Octal equivalent of decimal number 15 is:" +Integer.toOctalString(15));
      System.out.println("Octal equivalent of decimal number 24 is:" +Integer.toOctalString(24));
      System.out.println("Octal equivalent of decimal number 7 is:" +Integer.toOctalString(7));
      System.out.println("Octal equivalent of decimal number 5 is:" +Integer.toOctalString(5));
   }
}

输出

Octal equivalent of decimal number 15 is:17
Octal equivalent of decimal number 24 is:30
Octal equivalent of decimal number 7 is:7
Octal equivalent of decimal number 5 is:5

从八进制数制转换为十进制数制

转换的基本步骤

  • 将任何八进制数转换为十进制数:

  • 用八进制数的基数8的减小幂(直到幂为0)乘以十进制数中的每个数字。

  • 让我们通过一些示例来更好地理解这个概念。

示例1 - 让我们考虑一个八进制数 456 ,并找出它的等值十进制数。

456 = (4 * 8^2) + (5 * 8^1) + (6 * 8^0)
   = (4 * 64) + (5 * 8) + (6 * 1)
   = 256 + 40 + 6
   = 302

因此, 302 是八进制数 456 的等价十进制数。

示例2 - 让我们考虑另一个八进制数 152 并找出它的等价十进制数。

152 = (1 * 8^2) + (5 * 8^1) + (2 * 8^0)
   = (1 * 64) + (5 * 8) + (2 * 1)
   = 64 + 40 + 2
   = 106

因此, 106 是八进制数 152 的十进制等效数。

示例

以下程序使用自定义逻辑将十进制数转换为对应的八进制数。在名为OctalToDecimal1的类中创建了一个名为OctToDec的带有1个参数的方法,以演示八进制数系统的工作原理。在这里,将四个不同的八进制数作为实际参数传递给创建的方法,并将其转换为对应的十进制数,使用自定义逻辑。

//Java Program to illustrate the conversion of Octal to Decimal
//using custom logic
public class OctalToDecimal1 {
   //Declaring a method to perform a conversion
   public static int OctToDec(int oct) {
      //Declaring variable to store a decimal number
      int dec = 0;
      //Declaring variable to use in power
      int p = 0;
      //generating the logic
      while(true) {
         if(oct == 0) {
         break;
         } else {
            int t = oct % 10;
            dec += t * Math.pow(8, p);
            oct = oct / 10;
            p++;
         }
      }
      return dec;
   }
   public static void main(String args[]) {
      //displaying the result of the conversion
      System.out.println("Equivalent Decimal of octal number 673 is: "+OctToDec(673));
      System.out.println(" Equivalent Decimal of octal number 71 is: "+OctToDec(23));
      System.out.println("Equivalent Decimal of octal number 100 is: "+OctToDec(100));
      System.out.println("Equivalent Decimal of octal number 88 is: "+OctToDec(10));
   }
}

输出

Equivalent Decimal of octal number 673 is: 443
Equivalent Decimal of octal number 71 is: 19
Equivalent Decimal of octal number 100 is: 64
Equivalent Decimal of octal number 88 is: 8

示例

以下程序使用一个库函数将八进制数转换为对应的十进制数。在名为OctalToDecimal2的类中,将四个不同的八进制数作为输入传递给库函数Integer.parseInt(),然后将其转换为对应的十进制数。

//Java Program to demonstrate the conversion of an octal number into its equivalent decimal number using a library function
public class OctalToDecimal2 {
   public static void main(String args[]) {
      System.out.println("Equivalent Decimal of octal number 673 is " + Integer.parseInt("673",8));
      System.out.println("Equivalent Decimal of octal number 673 is " + Integer.parseInt("23",8));
      System.out.println("Equivalent Decimal of octal number 673 is " + Integer.parseInt("100",8));
      System.out.println("Equivalent Decimal of octal number 673 is " + Integer.parseInt("10",8));
   }
}

输出

Equivalent Decimal of octal number 673 is 443
Equivalent Decimal of octal number 673 is 19
Equivalent Decimal of octal number 673 is 64
Equivalent Decimal of octal number 673 is 8

结论

本文讨论了八进制数的使用。通过一些示例和Java程序,讨论了将八进制数转换成十进制数及其反之的方法。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程