Java程式演示八進位制整數的使用


八進位制整數是一種以8為基數,數字從0到7的數字系統。Int資料型別用於儲存八進位制數。

這裡將討論八進位制數系統的用法:

  • 將十進位制轉換為八進位制

  • 將八進位制轉換為十進位制。

從十進位制數系統到八進位制數系統的轉換

轉換基礎

  • 要將任何十進位制數轉換為其等效的八進位制數

  • 持續將十進位制數除以八進位制數系統的基數8,直到商為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的等效八進位制數。

示例

以下程式使用自定義邏輯將十進位制數轉換為其對應的八進位制數。在名為DecimalToOctal1的類中建立了一個名為DectoOctal且帶有一個引數的方法,以演示八進位制數系統的運作方式。這裡,十進位制數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且帶有一個引數的方法,以演示八進位制數系統的運作方式。這裡,四個不同的八進位制數作為實際引數傳遞給建立的方法,並使用自定義邏輯將其轉換為對應的十進位制數。

//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程式討論了將八進位制數系統轉換為十進位制數系統以及反向轉換的方法。

更新於:2023年4月12日

182 次檢視

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.