I just wrote a simple function to convert a number with any base into a base 10 number:
#include <stdio.h>
#include <math.h>
#include <string.h>
int tobase10(int *input, int size, int base);
int main() {
int input[256] = {2, 2, 1, 0};
printf("%d\n", tobase10(input, 4, 3));
printf("Press any key to continue ...\n");
getchar();
return 0;}
int tobase10(int *input, int size, int base) {
int result = 0;
int count = 0;
while((size+1) != 0) {
result = (input[count] * pow(base, (size-1))) + result;
count++;
size--; }
return result; }
I now want to modify it so it edits the input array to put every digit of the number in separate array block. Then I want it to return the number of digets there are. The only way I can think of doing this is puting the result in a string, use strlen() to get the size, and use a loop with atoi() to read it back into the input array. Is there any better, more elegant way to do this?
It seems to get ugly when I try.