📜  Java中的整数 toOctalString() 方法

📅  最后修改于: 2022-05-13 01:55:49.336000             🧑  作者: Mango

Java中的整数 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 = 126

Consider an integer a = 186 
Octal output = 272        

下面的程序说明了 Integer.toOctalString() 方法的工作:
程序 1:对于正整数:

Java
// 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));
    }
}


Java
// 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));
    }
}


Java
// 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));
    }
}


输出:
Integral Number = 527
Octal Number = 1017

程序 2:对于负整数:

Java

// 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 = -51
Octal Number = 37777777715

程序 3:对于十进制值或字符串:
注意:每当十进制数或字符串作为参数传递时,它都会返回导致类型不兼容的错误消息。

Java

// 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 output
2 errors