In Java, Integer.toOctalString(n) returns the value of n in base 8, using only the digits 0 to 7. Like the binary and hex versions, it treats the number as an unsigned 32-bit value and adds no prefix.
Octal is still common for Unix file permissions, where a mode such as 755 packs three groups of three bits.
A static method of java.lang.Integer: call it on the class, as Integer.toOctalString(...). Integer lives in java.lang, so no import is needed.
Syntax
One argument: the int to show.
Java
String a = Integer.toOctalString(8); // "10"
String b = Integer.toOctalString(493); // "755"
String c = Integer.toOctalString(-1); // "37777777777"
Call
What it does
Integer.toOctalString(n)
Octal text of the unsigned 32-bit value.
Integer.toString(n, 8)
Signed octal text, such as "-10" for -8.
Integer.parseInt(text, 8)
The reverse: reads octal text back into an int.
int mode = 0755;
An octal literal: a leading zero makes the number octal in source code.
Each octal digit stands for exactly three bits, so a 32-bit value needs up to 11 digits, and the first digit of a negative number is 2 or 3.
Library declarations
Java
// java.lang.Integer
public static String toOctalString(int i)
Parameters
Parameter
Description
i (int)
The number to show. Negative values are read as unsigned 32-bit numbers.
Return Value
Return Value
Description
String
The octal digits, 1 to 11 characters, without a prefix, sign, or leading zeros.