Java - Integer toOctalString() 方法



描述

Java Integer toOctalString() 方法將整數引數作為無符號整數以 8 進位制形式返回字串表示形式。

宣告

以下是 java.lang.Integer.toOctalString() 方法的宣告

public static String toOctalString(int i)

引數

i − 要轉換為字串的整數。

返回值

此方法返回引數表示的無符號整數值的 8 進位制(以 8 為基數)字串表示形式。

異常

獲取正整數的 8 進製表示示例

以下示例演示瞭如何使用 Integer toOctalString() 方法獲取指定 int 值的 8 進位制字串表示形式。我們建立了一個 int 變數併為其分配了一個正整數。然後使用 toOctalString() 方法列印結果。

package com.tutorialspoint;
public class IntegerDemo {
   public static void main(String[] args) {
      int i = 170;
      System.out.println("Number = " + i);
    
      /* returns the octal string representation of the given number */
      System.out.println("toOctalString = " + Integer.toOctalString(i));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Number = 170
toOctalString = 252

獲取負整數的 8 進製表示示例

以下示例演示瞭如何使用 Integer toOctalString() 方法獲取指定 int 值的 8 進位制字串表示形式。我們建立了一個 int 變數併為其分配了一個負整數。然後使用 toOctalString() 方法列印結果。

package com.tutorialspoint;
public class IntegerDemo {
   public static void main(String[] args) {
      int i = -170;
      System.out.println("Number = " + i);
    
      /* returns the octal string representation of the given number */
      System.out.println("toOctalString = " + Integer.toOctalString(i));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Number = -170
toOctalString = 37777777526

獲取正零整數的 8 進製表示示例

以下示例演示瞭如何使用 Integer toOctalString() 方法獲取指定 int 值的 8 進位制字串表示形式。我們建立了一個 int 變數併為其分配了一個零值。然後使用 toOctalString() 方法列印結果。

package com.tutorialspoint;
public class IntegerDemo {
   public static void main(String[] args) {
      int i = 0;
      System.out.println("Number = " + i);
    
      /* returns the octal string representation of the given number */
      System.out.println("toOctalString = " + Integer.toOctalString(i));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Number = 0
toOctalString = 0
java_lang_integer.htm
廣告