Hi, I've tried and tried to make my own hex to decimal (myhtoi) function but am not succeeding
here's what I've got so far:
Code:
// a program to convert hex code into its equivalent decimal value
// myhtoi.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
int myhtoi(char string[]); // prototype of function (myhtoi)
int main(void)
{
char hex[] = "0x00000110"; // declare string of hex digits
myhtoi(hex); // call the function (myhtoi)
return 0; // exit program
}
int myhtoi(char string[]) // now make the function (myhtoi)
{
int i, n; // used for counting and holding the value later
n = 0; // set the default value of n
for (i = 2; string[i] >= '0' && string[i] <= '9'; i++) // loop until condition is met
{
}
printf("That hex code in decimal is %d\n", n);
return 0;
}
I've tried:
Code:
n = 16 * n + (string[i] - '0');
but it only works for 1 digit only.
can anyone help me?
Thanks.