Java Integer toOctalString()方法
八进制是以8为基数的数字系统,在运算中使用数字0到7。八进制广泛用于PDP-8等系统的计算,IBM大型机采用12位、24位或36位字。
Java.lang.Integer.toOctalString()方法是Java中的一个内置函数,用于返回整数参数的字符串表示,作为基数8的无符号整数。
语法
public static String toOctalString(int num)
参数: 该方法接受一个整数类型的参数 num ,该参数需要转换为字符串。
返回值: 该函数返回整数参数的字符串表示,为基数8的无符号整数。
示例
Consider an integer a = 86 Octal output = 126Consider an integer a = 186 Octal output = 272
下面的程序说明了Integer.toOctalString()方法的工作情况:
程序1: 对于正整数。
// Java program to demonstrate working// of Integer.toOctalString() method import java.lang.*; public class Geeks { public static void main(String[] args) { int a = 527; System.out.println("Integral Number = " + a); // returns the string representation of the unsigned int value // represented by the argument in octal (base 8) System.out.println("Octal Number = " + Integer.toOctalString(a)); }}
输出
Integral Number = 527Octal Number = 1017
程序2: 对于负整数。
// Java program to demonstrate working// of Integer.toOctalString() method import java.lang.*; public class Geeks { public static void main(String[] args) { int a = -51; System.out.println("Integral Number = " + a); // Returns the string representation of the unsigned int value // in octal (base 8) System.out.println("Octal Number = " + Integer.toOctalString(a)); }}输出
Integral Number = -51Octal Number = 37777777715
程序3: 对于十进制数值或字符串:
注意: 每当十进制数字或字符串作为参数被传递时,它将返回一个错误信息,导致 类型不兼容。
// Java program to demonstrate working// of Integer.toOctalString() method import java.lang.*; public class Geeks { public static void main(String[] args) { double a = 18.71; // Returns the string representation of the unsigned int value // in octal (base 8) System.out.println("Octal output = " + Integer.toOctalString(a)); String b = "63"; // Returns the string representation of the unsigned int value // in octal (base 8) System.out.println("Octal output = " + Integer.toOctalString(a)); }}输出
prog.java:15: error: incompatible types: possible lossy conversion from double to int System.out.println("Octal output = " + Integer.toOctalString(a)); ^prog.java:21: error: incompatible types: possible lossy conversion from double to int System.out.println("Octal output = " + Integer.toOctalString(a)); ^Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output2 errors 