# Thread: Writing function which convert a string of hexadecimal to corresponding integer

1. ## Writing function which convert a string of hexadecimal to corresponding integer

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. 2. I don't know what you're talking about. I enabled all possible warnings while compiling this, but my compiler( GCC 5.4 ) kept completely silent.

Maybe you keep compiling a different version of the file?

Or maybe... are those two includes on the same line in your code? If yes, put them in separate lines. 3. No those includes are in different lines. Btw did it work? Mine just keeps on taking input even after sending \n 4. I am running on gcc 6.3.0 on Debian Linux.

pow() takes two doubles, and returns a double.

Is this a homework assignment? You do know that the Standard Library provides a function strtol() to perform the same function. 5. Originally Posted by Noobpati Btw did it work? Mine just keeps on taking input even after sending \n
Same. This happens because you check for equality before input! The only way this loop would stop is if it encountered a "random" newline in the array or after the program crashes due to buffer overflow. 6. It's ridiculous to pass hex into the function when it's only used as a local variable. Presumably you are meant to read the input string in main and then pass that string into the function.

Why do you have i and j in your input loop? They contain exactly the same value. You only need i. Actually, why are you reading the string this way at all? Why not read it all at once with %s?

It is pointless to store a bunch of values in arr and then add them up. Why not get rid of arr and simply add up the values as you calculate them?

You don't need to use pow. You can easily calculate the powers of 16 without it. Begin with the power value as 1 and start at the end of the string (with the rightmost character). As you move leftwards, multiply the power by 16.

In your countdown loop, you are bizarrely stopping when i == 1. You need to go down to (and including) 0.

Don't use numbers like 48, 55, and 87. Instead use '0', 'A'-10, and 'a'-10, respectively.

EDIT:
Now that I think about it, you don't need the power at all. Just read the chars from left to right (not backwards), determine the value of the char and then add it into num after multiplying num by 16.
Code:
```    int num = 0;
int sz = strlen(hex);

for (i = 0; i < sz; i++) {
int value;

// determine value of hex[i] ...

num = num * 16 + value;
}```
If you encounter a non-hex char, break out of the loop. 7. Thanks!
I just couldn't find a way to pass the string entered in the main to the function without also passing the string length but that would make the function a bit different than what was asked for. Popular pages Recent additions c-language, hexadecimal, int, kernighan, ritchie 