# decimal to hexadecimal conversion C++

• here is one way to convert a decimal no to hexadecimal notations:
• void dechex()
{
int temp = dec;
char s[16];
s[15] = '\0';
int res, i=14;
while((dec/16)!=0 || (dec%16)!=0)
{
// cout<<"dec"<<dec<<endl;
// cout<<(dec/2)<<endl; //DEBUG

res = dec%16;
if(res ==0)
s[i] = '0';
else if(res == 1)
s[i]='1';
else if(res == 2)
s[i]='2';
else if(res == 3)
s[i]='3';
else if(res == 4)
s[i]='4';
else if(res == 5)
s[i]='5';
else if(res == 6)
s[i]='6';
else if(res == 7)
s[i]='7';
else if(res == 8)
s[i]='8';
else if(res == 9)
s[i]='9';
else if(res == 10)
s[i]='A';
else if(res == 11)
s[i]='B';
else if(res == 12)
s[i]='C';
else if(res == 13)
s[i]='D';
else if(res == 14)
s[i]='E';
else if(res == 15)
s[i]='F';

// cout<<s[i]<<endl; //DEBUG

i--;
dec = dec/16;
}

• remaining code:

for(int k=0;k<=i;k++)
s[k] = '0';
cout<<"\nThe coded form in hex is: "<<s<<endl;
dec = temp;
}

int main()
{
decbinocthex d2(15);
d2.dechex();

return 0;
}
• WAP to convert distance in meter cm to distance in feet inches overloading assignment ( = ) operator:

#include<iostream>
#include<string>

using namespace std;

class ftin
{
private:
int feet;
float inch;
public:
ftin( int ft, float in)
{
feet = ft;
inch = in;

}

void display() const
{
cout<<"\nfeet:"<<feet<<"inch:"<<inch<<endl;

}

};

class mcm
{
private:
int meter;
float cm;

public:
mcm(int met, float centi)
{
meter  = met;
cm = centi;

}
void display() const
{
cout<<"\nmeter:"<<meter<<"cm:"<<cm<<endl;

}
operator ftin() const;
};

mcm :: operator ftin() const
{
int met = meter;
float centi = cm;

float total = (100*met) + cm;
float inch = total/2.54 ;
int feet = inch/12 ;
inch = inch - 12*2.54;
return ftin(feet, inch);

}

int main()
{
int meter;
float cm;

while(true)
{
cout<<"Enter distance in meter:\n";
cin>>meter;

cout<<"Enter distance in cm(0 to 100):\n";
cin>>cm;

cout<<"\ndistance entered: "<<meter<<" meter"<<cm<<" cm\n";

if(cm > 100)
{
meter++;
cm -= 100;
}

mcm m(meter, cm);

ftin f = m;
cout<<"\nDistance in feet inches is:"<<endl;
f.display();

cout<<"\n";

}
return 0;

}

Result: -

[fedora@nirmal-linux C++Assignments]\$ g++ disfeetmeterfuncoverld.cpp
[fedora@nirmal-linux C++Assignments]\$ ./a.out
Enter distance in meter:
1
Enter distance in cm(0 to 100):
100

distance entered: 1 meter100 cm

Distance in feet inches is:

feet:6inch:48.2602

Enter distance in meter: