I have been banging my head against this wall for so long I have been driven make my first StackOverflow post I am trying to convert a string representing any number in any base to base 10.
int* baseToDec(int base, char* value){
//after three hours its time for a full restart :( give it up for hour 4!!!
printf("%s has a length of %d\n", value, (int)strlen(value));
int* res = (int*)malloc(1 * sizeof(int));
int p = 1;
for(int i = strlen(value)-1; i >= 0; --i){ // moving backwards through the list
int digit; // there is something I dont understand going on with the 0
printf("value at i:%d %c\n",i, value[i]);
if(value[i] >= 48 && value[i] <= 57){
digit = value[i] - 48;
}
if(value[i] >= 65 && value[i] <= 90){
digit = value[i] - 55;
}
printf("%d * %d\n",digit, p);
*res += digit * p;
p = p * base;
}
return res;
from what I can tell the issues is with strings not being the length I expect (something to do with the end characters) when I run the code as is I get a lot of junk digits that make the number seem way larger than it is. All feedback is welcome I hope I did this right
when run with 33 (41 in octal) I get these results
41 has a length of 4
value at i:3
0 * 1 //notice this 0 and the following one, where did they come from???
value at i:2 1
1 * 8
value at i:1
1 * 64
value at i:0 4
4 * 512
2120