Decimal to octal conversion
To convert from decimal to octal, just use the online converter below. You will also learn how to do the calculation yourself. Finally, you will follow the implementation of the algorithm in C, to exercise your programming skills.
Online decimal to octal converter
How it works
If you are already familiar with the decimal to binary or decimal to hex conversion, this one will be very easy for you. It uses the same principle.
To convert from decimal to octal:
- Divide the decimal number by 8. Save the result and the remainder.
- Repeat 1, but this time, divide the result from the last calculation. Once again, save the result and the remainder. Repeat until the result reaches 0.
- Take the remainders in reversed order. Those remainders form the representation of the number in octal number system.
Here is an example with dec to octal:
= ? (oct)
1: 16 / 8 = 2 and remainder 0
2: Repeat 1, using the previous result:
2 / 8 = 0 and remainder 2. We reached 0, so we continue with 3
3: The remainders of the division are 0 and 2. And reversed: 20.
One more example:
100 / 8 = 12; rem 4
12 / 8 = 1; rem 4
1 / 8 = 0; rem 1
The remainders are 441. Their reversed order is 144. So:
To exercise what you just read, convert several numbers in the range 10-200. When you are ready, test your result with the converter above.
Implementing the algorithm with C
Note that this implementation is for learning purpose only. It
shows you how to write a program that follows the above mentioned
algorithm. C has a standard way for converting numbers from decimal
to octal, using the itoa function.
unsigned long decimal;
octal = decimalToOctal(decimal);
printf("%lu in dec equals %s in octal\n", decimal, octal);
char *decimalToOctal(unsigned long decimal)
char *octal = malloc(23);
if(octal == NULL)
octal += 22;
*octal-- = '\0';
if(decimal == 0)
*octal = '0';
while(decimal > 0)
remainder = (decimal % 8) + '0';
*octal-- = remainder;
decimal = decimal / 8;
implement the above algorithm, we need a string(char point) that will
hold the remainders. We begin, by allocating the necessary space for
the result: 23 bytes. The input data is at most 64 bits long. To hold
that we need at most 22 octal digits and one null character to
terminate the string.
We write the results in reversed order, so we won't need to
reverse them later. To do that, we move the pointer to its end and
insert the terminating symbol.
the actual conversion begins. In the while loop we divide the decimal
remainder of the division decimal % 8
is an integer from 0 to 7. To convert that remainder to its char
representation we sum it with the character '0'. The result is the ASCII
code of the digit. If you are not familiar with char arithmetic, you
might want to check out this lesson:characters in C.
save the remainder in the current position of the char pointer. Then we
move the pointer one symbol to the left. We do these two operations
with the single line:
*octal-- = remainder;
Then we divide the decimal number and close the loop. When we are
done with the divisions the pointer is one symbol to the left of the
result. We correct this ( octal++; ) and
return the result.
The source code of the example is available on our GitHub.
.. or you can
download it from here: decimal-to-octal.zip
See also other