首頁  >  文章  >  Java  >  Java程式範例,說明八進位整數的使用方法

Java程式範例,說明八進位整數的使用方法

WBOY
WBOY轉載
2023-08-28 18:05:071220瀏覽

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 的等價八進位數。

Example

的中文翻譯為:

範例

以下程式使用自訂邏輯將十進制數轉換為其對應的八進位數。在名為 DecimalToOctal1 的類別中建立帶有 1 個參數的名為 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

Example

的中文翻譯為:

範例

以下程式使用函式庫函數將十進制數轉換為對應的八進位數。在一個名為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的等價十進位數。

Example

的中文翻譯為:

範例

以下程式使用自訂邏輯將十進制數轉換為其對應的八進位數。在名為 OctalToDecimal1 的類別中建立帶有 1 個參數的名為 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

Example

的中文翻譯為:

範例

以下程式使用函式庫函數將八進制數轉換為對應的十進制數。在一個名為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程式討論了八進制和十進制之間的轉換方法。

以上是Java程式範例,說明八進位整數的使用方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除