八进制是以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 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 = 527 Octal 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 = -51 Octal 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 output 2 errors
相关用法
- Java Integer sum()用法及代码示例
- Java Integer.numberOfLeadingZeros()用法及代码示例
- Java Integer hashCode()用法及代码示例
- Java Integer decode()用法及代码示例
- Java Integer floatValue()用法及代码示例
- Java Integer signum()用法及代码示例
- Java Integer compare()用法及代码示例
- Java Integer doubleValue()用法及代码示例
- Java Integer compareTo()用法及代码示例
- Java Integer shortValue()用法及代码示例
- Java Integer bitCount()用法及代码示例
- Java Integer byteValue()用法及代码示例
- Java Integer.numberOfTrailingZeros()用法及代码示例
- Java Integer highestOneBit()用法及代码示例
注:本文由纯净天空筛选整理自ankita_chowrasia大神的英文原创作品 Integer toOctalString() Method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。