#include<stdio.h>
#include<math.h>
main()
{
    int i=0,j,c,s[100];
    double t=0;
    while((c=getchar())!='\n')            
    {
        s[i]=c;
        ++i;
    }
    for(j=i;j>0;--j)
    {
        t=t+(s[i-1]*(pow(16,(4-((double)i)))));
        
        --i;
    }

    printf("%d",(int)t);
    return 0;
}

HI, i need to write a program that converts an hexadecimal number into integer form (for convenience i'm only considering hexadecimal no. without alphabets from a to f).
The program which i've written above is for the same conversion but the output is not right. I tried to find the error but failed. It would be a great help if you give suggestions to correct the above code. Pls do not ask me to write the program using some other method.:?:

Recommended Answers

All 2 Replies

Can you explain what 4-((double)i) is supposed to do?

check it now

#include<stdio.h>
#include <stdlib.h>
#include<math.h>
# include <iostream>
using namespace std;
int main()
{
    int i=0,j,c,s[100];
    double t=0;
    while((c=getchar())!='\n')            
    {
        s[i]=c;
		s[i] = s[i] - 48;
        ++i;
    }
	int x = 0;
    for(j=i-1;j>=0;j--)
    {
        //t=t+(s[i-1]*(pow(16,(4-((double)i)))));   
		t = t + s[j]*(pow(double(16),double(x)));
		x++;
    }
	cout<<t;
    return 0;
}
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.