Search code examples
c++if-statementhexdecimaldata-conversion

Hexadecimal to decimal conversion problem.Also, how to convert a char number to an actual int number


Please help me to identify the error in this program, as for me it's looking correct,I have checked it,but it is giving wrong answers. In this program I have checked explicitly for A,B,C,D,E,F,and according to them their respective values.

[Edited]:Also,this question relates to how a character number is converted to actual integer number.

#include<iostream>
#include<cmath>
#include<bits/stdc++.h>
using namespace std;
void convert(string num)
{
   long int last_digit;
    int s=num.length();
    int i;
    long long int result=0;
    reverse(num.begin(),num.end());                 
    for(i=0;i<s;i++)
    {
        if(num[i]=='a' || num[i]=='A')
        {
            last_digit=10;
            result+=last_digit*pow(16,i);
        }
        else if(num[i]=='b'|| num[i]=='B')
        {
            last_digit=11;
            result+=last_digit*pow(16,i);
        }
        else if(num[i]=='c' || num[i]=='C')
        {
            last_digit=12;
            result+=last_digit*pow(16,i);
        }
        else if(num[i]=='d'|| num[i]=='D' )
        {
            last_digit=13;
            result+=last_digit*pow(16,i);
        }
        else if(num[i]=='e'|| num[i]=='E' )
        {
            last_digit=14;
            result+=last_digit*pow(16,i);
        }
        else if(num[i]=='f' || num[i]=='F')
        {
            last_digit=15;
            result+=last_digit*pow(16,i);
        }
        else {
            last_digit=num[i];
        result+=last_digit*pow(16,i);
        }
    }
    cout<<result;
}
int main()
{
    string hexa;
    cout<<"Enter the hexadecimal number:";
    getline(cin,hexa);
    convert(hexa);
}

Solution

  • Your problem is in the elsecase in which you convert num[i] from char to its ascii equivalent. Thus, for instance, if you try to convert A0, the 0is converted into 48 but not 0. To correct, you should instead convert your num[i] into its equivalent integer (not in asci).

    To do so, replace :

    else {
                last_digit=num[i];
            result+=last_digit*pow(16,i);
    

    with

      else {
                last_digit = num[i]-'0';
                result+=last_digit*pow(16,i);
        }
    

    In the new line, last_digit = num[i]-'0'; is equivalent to last_digit = (int)num[i]-(int)'0';which substracts the representation code of any one-digit-number from num[i] from the representation code of '0'

    It works because the C++ standard guarantee that the number representation of the 10 decimal digits are contiguous and in incresing order (official ref iso-cpp and is stated in chapter 2.3 and paragraph 3

    Thus, if you take the representation (for instance the ascii code) of any one-digit-number num[i] and substract it with the representation code of '0' (which is 48 in ascii), you obtain directly the number itself as an integer value.

    An example of execution after the correction would give:

    A0
    160
    
    F5
    245
    

    A small codereview: You are repeating yourself with many result+=last_digit*pow(16,i);. you may do it only once at the end of the loop. But that's another matter.