Practical Coding in Java

Learn to write and validate your own code

Darren Kessner, PhD

(revised September 1, 2025)

Previous: Converting between Hex and Binary

Octal

Octal is base 8, and we use the eight numeric symbols 0-7.

\[ \begin{array}{|c|c|r|r|} \hline \text{decimal} & \text{hexadecimal} & \text{binary} & \text{octal} \\ \hline 0 & 0 & 0 & 0 \\ 1 & 1 & 1 & 1 \\ 2 & 2 & 10 & 2 \\ 3 & 3 & 11 & 3 \\ 4 & 4 & 100 & 4 \\ 5 & 5 & 101 & 5 \\ 6 & 6 & 110 & 6 \\ 7 & 7 & 111 & 7 \\ 8 & 8 & 1000 & 10 \\ 9 & 9 & 1001 & 11 \\ 10 & A & 1010 & 12 \\ 11 & B & 1011 & 13 \\ 12 & C & 1100 & 14 \\ 13 & D & 1101 & 15 \\ 14 & E & 1110 & 16 \\ 15 & F & 1111 & 17 \\ \hline \end{array} \]

Example

\[ \begin{aligned} \text{234}\,_\text{OCT} &= \underset{8^2}{\fbox{ 2 }} \underset{8^1}{\fbox{ 3 }} \underset{8^0}{\fbox{ 4 }} \\ &= 2 \cdot 8^2 + 3 \cdot 8^1 + 4 \cdot 8^0 \\ &= 2 \cdot 64 + 3 \cdot 8 + 4 \cdot 1 \\ &= 156 \, _\text{DEC} \end{aligned} \]

One octal digit corresponds to 3 bits (\(2^3 = 8\)), so conversions between octal and binary are also straightforward.

Example

\[ \begin{aligned} \fbox{ 2 } \fbox{ 3 } \fbox{ 4 } &= \underset{2}{\fbox{010}} \underset{3}{\fbox{011}} \underset{4}{\fbox{100}} \\ \\ 234\,_\text{OCT} &= 010 \, 011 \, 100 \, _\text{BIN} \end{aligned} \]

To convert decimal to octal, pretend you are making change.

Example

To convert \(100_\text{DEC}\) to octal, we first ask how many 64’s it contains (1, with remainder 36). Then we ask how many 8’s are in 36 (4, with remainder 4).

\[ \begin{aligned} 100\,_\text{DEC} &= 1 \cdot 64 + 4 \cdot 8 + 4 \cdot 1 \\ &= 144\,_\text{OCT} \\ \end{aligned} \]


Next: