Decimal to Octal conversion in C++

This tutorial would help in learning the following:

  • Conversion of numbers from decimal-system (using 0 to 9) to octal-system (using 0 to 7).
  • Conversion of characters into their respective ASCII codes.
  • Code for Decimal to Octal conversion in C++.

Convert Decimal to Octal in C++

The decimal number system uses 10 digits (0 to 9) to represent a number while the octal number system uses 7 digits (0 to 7). The following algorithm is used to convert a decimal-number into an octal-number:

let the number be 63

  1. divide the number by 8 (integer division) and note down the remainder and keep the quotient
    63/8: quotient -> 7, remainder -> 7
  2. repeat step 1 on the quotient till there comes another quotient which is 0, note down the remainders obtained in right-to-left fashion
    7/8: quotient -> 0, remainder -> 7; remainder-list -> 77
  3. the remainders, placed in the right-to-left fashion form the binary representation
    (63)10~ (77)8

Each character used in computing is assigned a special ASCII code (decimal-number-system); hence, to convert a character into octal-form, its ASCII code can be obtained and it can be converted.
Note, there can be different encoding systems on different machines.

CPP Program to convert decimal to octal

#include <stdio.h>
#define string "Code Speedy" //this value can be changed to obtain the octal code for any collection of characters
#define number 63//this value can be changed to obtain the octal form of any non negative integer
void stringToOctal(char *);
int octal(int);
int main()
{
    char *p=string;
    printf("The octal form of %d is %d\n",number,octal(number));
    printf("The octal equivalent of %s is:\n",string);
    stringToOctal(p);
    return 0;
}
void stringToOctal(char *p)//converts a line to binary code
 {
     char c;//characters can be treated as integers in C++
     while((c=*(p++))!='\0'){//each character is extracted and its binary code is printed
         printf("%d.",octal(c));//a full-stop follows after each individual binary code
     }
     printf("\n");
 }
int octal(int x)//converts an integer to its equivalent binary form
 {
     if(x<=1)return x;
     else return (x%8) + 10*octal(x/8);
 }

Output:

The octal form of 63 is 77
The octal equivalent of Code Speedy is:
103.157.144.145.40.123.160.145.145.144.171.

Summary:

int octal(int):

  • a recursive approach is taken.
  • a remainder (x%8) is taken at each call and added next to the remainder of the next call.
  • multiplication with 10 is done to ensure that the addition is done in right-to-left fashion and not simple summation.

void stringToOctal(char *):

  • a pointer to a collection of characters is taken as parameter.
  • as long as the pointer does not point to a null character, the octal form of the current character is printed.

Also read:

Convert First letter of each word to uppercase in C++

Leave a Reply

Your email address will not be published. Required fields are marked *