how would i write a function that parses a hex number as a string into a decimal integer. I have the following done i just need help finishing it. any help will be appreciated

#include <iostream>
#include <string>
using namespace std;

int parseHex(const string &hexString)

int main()
{
  cout << "Enter a hex number: ";
  string hexString;
  cin >> hexString;
  cout <<"The number in decimal is"
       << parseHex(hexString) << endl;

  return 0;
}

int parseHex(const string &hexString)
{
  int value = hexString[0]

Recommended Answers

All 5 Replies

The conversion of hexa to decimal follows in thid manner

Hex value =A48
Decimal value =8*16^0 +4*a6 ^1 +A*16^2+...

so first thing you need to do is to convert string value to integer using

int result =atoi(hexString)

Then you can keep on dividing the integer by 10 and getting the value like

int count =0; int sum =0;
 do
{
int  multiple = result%10;
sum =sum + multiple*(pow(16,count)); 
count++;
result=result/10;
}while(result!=0)

You need to include math.h and may have to do some modifications as i have not complied the code. You can use the above idea to convert from any type to decimal.

hey hello,
please help me to find out conversion of hex to dec

sheetalphapale if you have a question, please start your own thread. One of our rules is not to hijack old threads (this is 4 years old), especially when you have a question. That being said, here, this might help you:

You can make use of the stringstream class from c++ (Click Here).

Here's a small example:

int a = 15;
int b = 0xF;
int c;

cout << a << endl;          // will print 15
cout << b << hex << endl; // will print 15

stringstream stream;
stream << b;
stream >> hex >> c;

cout << c << endl;          // will print 15

Hello Sir,
I have a problem to convert hex value(0X16) to decimal value(22).I used formula to convert it but it shows answer is again in hex value so where did i mistake??can you suggest me.I have attached code also
void hextodec(unsigned int freq)

void hextodec(unsigned int freq)
{
    unsigned char val_lwr = 0, val_hgr = 0;

                unsigned int sum=0;
                unsigned int count=0;
                unsigned int result;
                unsigned int result1;
                result1=freq;
                val_hgr=result1&0XF0;
                val_hgr=val_hgr>>4;
                result=val_hgr;
            do
             {          
        sum=sum+ result*(pow(16,count));
            count++;
            result=result1&0X0F;
            }while(count<2); 
}

Hello Sir,
I have a problem to convert hex value(0X16) to decimal value(22).I used formula to convert it but it shows answer is again in hex value so where did i mistake??can you suggest me.I have attached code also

void hextodec(unsigned int freq)
{
    unsigned char val_lwr = 0, val_hgr = 0;

                unsigned int sum=0;
                unsigned int count=0;
                unsigned int result;
                unsigned int result1;
                result1=freq;
                val_hgr=result1&0XF0;
                val_hgr=val_hgr>>4;
                result=val_hgr;
            do
             {          
        sum=sum+ result*(pow(16,count));
            count++;
            result=result1&0X0F;
            }while(count<2); 

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.