Skip to content

Octal

Octal is a base-8 number system that uses eight digits: 0, 1, 2, 3, 4, 5, 6, and 7. Octal numbers are commonly used in computing, especially in the Unix and Linux operating systems, for representing file permissions and other system-related values. Each digit takes up 3 bits.

In the octal system, each digit represents powers of 8, similar to how each digit in the decimal system represents powers of 10. Octal is used less frequently than binary or hexadecimal in modern computing but is still relevant in certain contexts.

Conversion from Decimal

Converting between decimal (base-10) and octal can be done using various programming languages.

Here's some rough math explaining how it's done:

Let n=the decimal number to convertLet octal=empty stringWhile n>0octal=(nmod8)+octaln=n/8Return octal

Here's an example of how you can implement the conversion yourself:

For the curious

python
def decimal_to_octal(decimal: int) -> str:
    octal = ""
    while decimal > 0:
        octal = str(decimal % 8) + octal
        decimal = decimal // 8
    return octal

print(decimal_to_octal(20))  # 24
js
function decimalToOctal(decimal) {
    let octal = "";
    while (decimal > 0) {
        octal = (decimal % 8) + octal;
        decimal = Math.floor(decimal / 8);
    }
    return octal;
}

console.log(decimalToOctal(20)); // 24
cpp
#include <iostream>
#include <string>
using namespace std;

string decimalToOctal(int decimal) {
    string octal = "";
    while (decimal > 0) {
        octal = to_string(decimal % 8) + octal;
        decimal /= 8;
    }
    return octal;
}

int main() {
    cout << decimalToOctal(20) << endl; // 24
}
rust
fn decimal_to_octal(decimal: i32) -> String {
    let mut octal = String::new();
    let mut decimal = decimal;
    while decimal > 0 {
        octal = format!("{}{}", decimal % 8, octal);
        decimal /= 8;
    }
    octal
}

fn main() {
    println!("{}", decimal_to_octal(20)); // 24
}

For the practical

python
print(oct(20))  # 0o24
js
console.log((20).toString(8)); // 24
cpp
#include <iostream>
#include <bitset>
using namespace std;

int main() {
    cout << oct << 20 << endl; // 24
}
rust
fn main() {
    println!("{:o}", 20); // 24
}

Conversion from Binary

Converting between binary (base-2) and octal can be done using various programming languages.

Once again, here's some math:

Say for example we have the binary number 1101010111102. We can split it into groups of 3 bits from the right, padding with 0s if necessary:

110Group 1 101Group 2 011Group 3 110Group 4

Then we can convert each group to its octal equivalent:

For Group 1: (110)2=(68)For Group 2: (101)2=(58)For Group 3: (011)2=(38)For Group 4: (110)2=(68)

As for how to convert each group to its octal equivalent:

Let b1b2b3=the binary number to convertLet octal=(b1×22)+(b2×21)+(b3×20)

After going through each group, we can concatenate the octal representations to get the final octal number:

(110101011110)2=(6536)8

Here's an example of how you would implement the conversion yourself:

python
def binary_to_octal(binary: str) -> str:
		octal = ""
		binary = binary.zfill(3 * ((len(binary) + 2) // 3))
		for i in range(0, len(binary), 3):
				octal += str(int(binary[i:i + 3], 2))
		return octal

print(binary_to_octal("110101011110"))  # 6536
js
function binaryToOctal(binary) {
		let octal = "";
		binary = binary.padStart(3 * Math.ceil(binary.length / 3), "0");
		for (let i = 0; i < binary.length; i += 3) {
				octal += parseInt(binary.slice(i, i + 3), 2);
		}
		return octal;
}

console.log(binaryToOctal("110101011110")); // 6536
cpp
#include <iostream>
#include <bitset>
using namespace std;

string binaryToOctal(string binary) {
		string octal = "";
		binary = binary.insert(0, 3 - (binary.length() % 3), '0');
		for (int i = 0; i < binary.length(); i += 3) {
				octal += to_string(bitset<3>(binary.substr(i, 3)).to_ulong());
		}
		return octal;
}

int main() {
		cout << binaryToOctal("110101011110") << endl; // 6536
}
rust
fn binary_to_octal(binary: &str) -> String {
		let mut octal = String::new();
		let binary = binary.chars().rev().collect::<String>().chars().rev().collect::<String>();
		let binary = format!("{:0>1$}", binary, 3 * ((binary.len() + 2) / 3));
		for i in (0..binary.len()).step_by(3) {
				octal.push_str(&format!("{}", u8::from_str_radix(&binary[i..i + 3], 2).unwrap()));
		}
		octal
}

fn main() {
		println!("{}", binary_to_octal("110101011110")); // 6536
}

And of course, I'd understand if you'd rather use built-in functions:

python
print(oct(int("110101011110", 2)))  # 0o6536
js
console.log((parseInt("110101011110", 2)).toString(8)); // 6536
cpp
#include <iostream>
#include <bitset>
using namespace std;

int main() {
		cout << oct << bitset<12>("110101011110").to_ulong() << endl; // 6536
}
rust
fn main() {
		println!("{:o}", u16::from_str_radix("110101011110", 2).unwrap()); // 6536
}

Conversion from Hexadecimal

Converting between hexadecimal (base-16) and octal can also be done using various programming languages (shocker, I know).

But yes, before that, the math:

Let's say you have a hexadecimal number, 420AF16. To convert this hexadecimal number to octal, you can first convert it to binary, then convert the binary number to octal (you may refer to the previous section as well):

401002001000000A1010F1111

Then you can convert the binary number to octal (refer to the above section for the steps):

(01000010000010101111)2=(010)(000)(100)(101)(011)(111)2=(1020257)8