Octal To Hexadecimal program in C


Implementation

Implementation of this algorithm is given below −


#include <stdio.h>

int string_length(char s[])
{
	int i=0;

	while(s[i]!='\0')
		i++;
		
return i;	
}

int octal2decimal(char n[])
{
	int i,decimal,mul=0;

	for(decimal=0,i=string_length(n)-1;i>=0;--i,mul+=3)
		decimal+=(n[i]-48)*(1<<mul);

	return decimal;
}

int main()
{   char n[] = "7";
    char bits[100];
   int i,j,d;
   

   string_reverse(n);
      
   for(i=j=0;n[i]!='\0';++i)
     for(d=n[i]-48;d!=0;d/=2)
         bits[j++]=d%2+48;
   
   bits[j]='\0';

   string_reverse(bits);

   printf("Binary equivalent of %s is : %s\n", n, bits);
return 0;
}

Output

Output of the program should be −

Binary equivalent of 7 is : 111
Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements