Convert Octal to Binary

The C program carry out number system conversion from a octal value (base-8) to binary numbers (base-2) is called octal to binary conversion. A octal number is represented by binary 3 bits.


octal to binary

Octal number system

The octal number system has the following unique digits.
{0, 1, 2, 3, 4, 5, 6, 7}

As the octal number system has the 8 unique digits, it is called base 8 or radix 8 number system.

Each octal digit can be represented by binary 3 bits.


Binary number system

The Binary number system has the following unique digits.
{0, 1}

As the binary number system has the 2 unique digits, it is called base 2 or radix 2 number system.

The computer can only understand this number system and is operated by this binary number system, therefore it accepts data,instruction and everything in binary numbers format.


Octal to Binary conversion example

    
       The given octal number is 653 and 
           converts it into binary numbers

         octal = 653 , binary = ?
            
         split each digit of octal given number and
            carried out decimal to 3 bits binary conversion on each octal digit  
                

       octal =     6  |  5  |  3
       binary =   110   101   011

      if octal = 653 and equivalent binary = 110101011
 
  

Octal to Binary - C program code

The C program converts octal number into binary number. The program reads octal number from user and executes conversion process and returns binary numbers as result.
 

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>


char* dec2bin(int decval) 
{
    char *bits=(char*) malloc(sizeof(char)*4);
    int n=0;  int bpo=3;
    for(int n=0;n<bpo;n++)   
     {     
        int reminder= decval %2;
        decval  = decval /2;       
        bits[bpo-n-1] = (reminder==1) ? '1' : '0';        
     }  
  bits[bpo] = '\0';  
  return bits;
}

int oct2dec(char ox) {

   int decval=0;
   
   switch (ox) 
    {   
  case '1' :
     decval=1;
  break;
  case '2':
     decval=2;
  break;
 case '3':
     decval=3;
  break;
 case '4':
     decval=4;
  break;
 case '5':
     decval=5;
  break;
 case '6':
     decval=6;
  break;
 case '7':
     decval=7;
  break;     
 default:
        decval =0;  
 }
 
 return decval;
   
}

int main() {

   int mxoct =5; 
   char oct[mxoct+1];    
   char *bits;    
   
   printf("\n Octal to Binary Conversion");   
   printf("\n Enter octal value :") ;
   scanf("%s",&oct);
   
   for(int n=0;n<strlen(oct);n++) {
   
      int decval = oct2dec(oct[n]);   
      char *b3s = dec2bin(decval);
   strcat(bits,b3s);
   //printf("\n %c \t %d \t %s",oct[n],decval,dec2bin(decval));
   
   }
   
   printf("\nOctal value : %s", oct); 
   printf("\nBinary bits : %s ",bits);
   return 0;
}

 

Octal to Binary conversion - C program Output


 Octal to Binary Conversion
 Enter octal value :546

 Octal value : 546
 Binary bits :101100110





Download c program source code

Comments

Popular posts from this blog

Hexadecimal to Binary Conversion

C program Simple Interest and Compound Interest