I was trying to write a function int htoi(char )to convert a string of hexadecimal to its decimal integer value.
Here's the code:
Code:
#include <stdio.h>#include <stdlib.h>
#include <math.h>
#define MAX 100
int htoi(char hex[]);
int main()
{
int result;
char hex[MAX];
result = htoi(hex);
printf("%d", result);
}
int htoi(char hex[])
{
int num,i,j,k,h,szarray; //szarray is the length of string of hexadecimal
for(i=0,j=0; hex[i] != '\n' ; ++i)
{
scanf("%c",&hex[i]);
j++;
}
szarray = j;
int arr[szarray-2]; //We define arr array to store the values of the input hexadecimal numbers
h=0;
j=0;
for (i=szarray-1 ; i>1; --i,++h,++j)
{
if (hex[i]>= 'A' && hex[i]<= 'F')
{
arr[j] = pow(16,h)*(hex[i]-55);
}
else if (hex[i]>= 'a' && hex[i]<= 'f')
{
arr[j] = pow(16,h)*(hex[i]-87);
}
else if (hex[i]>= '0' && hex[i]<= '9')
{
arr[j] = pow(16,h)*(hex[i]-48);
}
}
num = 0;
for (k=0; k < szarray - 2 ; ++k) //sums all the numbers contained in the array arr//
{
num = num + arr[k];
}
return num;
}
Well while compiling my IDE gave
warning that: in htoi format %c expects argument type "char *" but argument 2 has type int
Can anyone explain me what this means? The program crashes after inputting a string of hexadecimal so I guess that has something to do with what's wrong with my program.