Java 程序将 int 转换为八进制字符串


Java 中的 Integer.toOctalString() 方法将 int 转换为八进制字符串。

假设我们是以下整数值。

int val1 = 9;
int val2 = 20;
int val3 = 2;

将上述 int 值转换为八进制字符串。

Integer.toOctalString(val1);
Integer.toOctalString(val2);
Integer.toOctalString(val3);

示例

 动态演示

public class Demo {
   public static void main(String[] args) {
      int val1 = 9;
      int val2 = 20;
      int val3 = 30;
      int val4 = 78;
      int val5 = 2;
      System.out.println("Converting integer "+val1+" to Octal String:       "+Integer.toOctalString(val1));
      System.out.println("Converting integer "+val2+" to Octal String: "+Integer.toOctalString(val2));
      System.out.println("Converting integer "+val3+" to Octal String: "+Integer.toOctalString(val3));
      System.out.println("Converting integer "+val4+" to Octal String: "+Integer.toOctalString(val4));
      System.out.println("Converting integer "+val5+" to Octal String: "+Integer.toOctalString(val5));
   }
}

输出

Converting integer 9 to Octal String: 11
Converting integer 20 to Octal String: 24
Converting integer 30 to Octal String: 36
Converting integer 78 to Octal String: 116
Converting integer 2 to Octal String: 2

更新时间: 26-Jun-2020

617 人已查看

开启你的 职业

通过完成课程获得认证

开始
广告