Here is a C++ program to convert hexadecimal to octal
How to convert hexadecimal to octal(base 16 to base 8).
At first convert each digit in hexadecimal to four bit binary number and than convert binary to octal. For example convert (A4)16 to octal.
(A4)16 = (10100100)2
(10100100)2 = (244)8
(A4)16 = .(244)8
Method 1 : C++ to convert hexadecimal to octal
Method 2 : C++ to convert hexadecimal to octal using recursion
How to convert hexadecimal to octal(base 16 to base 8).
At first convert each digit in hexadecimal to four bit binary number and than convert binary to octal. For example convert (A4)16 to octal.
(A4)16 = (10100100)2
(10100100)2 = (244)8
(A4)16 = .(244)8
Method 1 : C++ to convert hexadecimal to octal
#include<iostream>
#include<math.h>
#include<stdio.h>
using namespace std;
int main()
{
long i,y,hexa[20],binary[20],rem,octal=0,z=0,temp=1,w=0;
char input[20];
cout<<"Enter the number in Hexadecimal ";
gets(input);
for(y=0;input[y]!='\0';y++);
for(i=y-1;i>=0;i--)
{
if(input[i]>='A'&&input[i]<='F')
hexa[i]=input[i]-55;
else if(input[i]>='a')
hexa[i]=input[i]-87;
else
hexa[i]=input[i]-48;
for(int y=0;y<4;y++)
{
rem=hexa[i]%2;
hexa[i]/=2;
binary[z++]=rem;
}
}
for(int i=0;i<z;)
{
for(int y=0;y<3;y++)
{
if(i>=0)
octal+=binary[i++]*pow(2,y);
}
w+=octal*temp;
octal=0;
temp*=10;
}
cout<<"Octal equivalent : "<<w;
return 0;
}
#include<math.h>
#include<stdio.h>
using namespace std;
int main()
{
long i,y,hexa[20],binary[20],rem,octal=0,z=0,temp=1,w=0;
char input[20];
cout<<"Enter the number in Hexadecimal ";
gets(input);
for(y=0;input[y]!='\0';y++);
for(i=y-1;i>=0;i--)
{
if(input[i]>='A'&&input[i]<='F')
hexa[i]=input[i]-55;
else if(input[i]>='a')
hexa[i]=input[i]-87;
else
hexa[i]=input[i]-48;
for(int y=0;y<4;y++)
{
rem=hexa[i]%2;
hexa[i]/=2;
binary[z++]=rem;
}
}
for(int i=0;i<z;)
{
for(int y=0;y<3;y++)
{
if(i>=0)
octal+=binary[i++]*pow(2,y);
}
w+=octal*temp;
octal=0;
temp*=10;
}
cout<<"Octal equivalent : "<<w;
return 0;
}
Method 2 : C++ to convert hexadecimal to octal using recursion
#include<iostream>
#include<math.h>
#include<string.h>
using namespace std;
int bin=0;
void binary(int n,int temp,int z)
{
if(z==4)
return ;
bin+=(n%2)*temp;
binary(n/2,temp*10,z+1);
}
void hex(char *str,int i,int j)
{
if(i<0)
return ;
if(str[i]>=65&&str[i]<=70)
binary(str[i]-55,j,0);
else if(str[i]>=97)
binary(str[i]-87,j,0);
else
binary(str[i]-48,j,0);
hex(str,i-1,j*10000);
}
int deci(int n,int i)
{
if(n==0)
return 0;
return (n%10)*pow(2,i)+deci(n/10,i+1);
}
void octal(int n)
{
if(n==0)
return;
octal(n/1000);
cout<<deci(n%1000,0);
}
int main()
{
char str[10];
cout<<"Enter Hexadecimal number : ";
cin>>str;
cout<<"Octal equivalent : ";
hex(str,strlen(str)-1,1);
octal(bin);
return 0;
}
#include<math.h>
#include<string.h>
using namespace std;
int bin=0;
void binary(int n,int temp,int z)
{
if(z==4)
return ;
bin+=(n%2)*temp;
binary(n/2,temp*10,z+1);
}
void hex(char *str,int i,int j)
{
if(i<0)
return ;
if(str[i]>=65&&str[i]<=70)
binary(str[i]-55,j,0);
else if(str[i]>=97)
binary(str[i]-87,j,0);
else
binary(str[i]-48,j,0);
hex(str,i-1,j*10000);
}
int deci(int n,int i)
{
if(n==0)
return 0;
return (n%10)*pow(2,i)+deci(n/10,i+1);
}
void octal(int n)
{
if(n==0)
return;
octal(n/1000);
cout<<deci(n%1000,0);
}
int main()
{
char str[10];
cout<<"Enter Hexadecimal number : ";
cin>>str;
cout<<"Octal equivalent : ";
hex(str,strlen(str)-1,1);
octal(bin);
return 0;
}
No comments:
Post a Comment