Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

how to convert hexadecimal to decimal in c++

#include<bits/stdc++.h>
using namespace std;

int hexadecimaltoDecimal(string n)
{
    int ans = 0;
    int x = 1;
    int s = n.size();
    for( int i=s-1 ; i>=0 ; i-- )
    {
        if( n[i] >= '0' && n[i] <= '9' )
        {
            ans += x*( n[i] - '0' );
        }
        else if( n[i] >= 'A' && n[i] <= 'F' )
        {
            ans += x*( n[i] - 'A' + 10 );
        }
        x *= 16;
    }
return ans;
}

int main()
{
    string n;
    cout<<"Input a Hexa Decimal number :"<<endl;
    cin>>n;
    cout<<"The Decimal number will be :"<<endl;
    cout<<hexadecimaltoDecimal(n)<<endl;
return 0;
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #convert #hexadecimal #decimal
ADD COMMENT
Topic
Name
4+2 =