=> Program :
#include<iostream>
#include<math.h>
using namespace std;
string low2up(string passStr){
string changeStr=passStr;
for(int i=0;i<changeStr.size();i++){
if(passStr[i]>=97 && passStr[i]<=122)
{
changeStr[i]=changeStr[i]-32;
}
}
return changeStr;
}
bool validHexVal(string checkSr){
bool checkValid;
int countNum=0;
for(int i=0;i<checkSr.size();i++){
if(checkSr[i]>=65 && checkSr[i]<=70)
{
countNum++;
}
if(checkSr[i]>=48 && checkSr[i]<=57){
countNum++;
}
}
if(checkSr.size()==countNum){
return true;
}else{
return false;
}
}
int main(){
int decNum=0,val;
string hexNum,upperHexNum;
cout<<"Enter the hex decimal number "<<endl;
cin>>hexNum;
upperHexNum=low2up(hexNum);
int n=upperHexNum.size();
if(validHexVal(upperHexNum)){
for(int i=n-1;i>=0;i--){
if(upperHexNum[i]>=65 && upperHexNum[i]<=70)
{
val=char(upperHexNum[i])-55;
}else{
val=char(upperHexNum[i])-48;
}
decNum=decNum+val*pow(16,n-(i+1));
}
cout<<"Decimal Value : "<<decNum<<endl;
}else{
cout<<"Not Valid Hexadecimal Value";
}
}
Comments
Post a Comment