Last active
September 8, 2016 05:35
-
-
Save twof/b0c9f7a498df9e0ed9ee8da87181d4a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main(void) | |
{ | |
char base_digits[16] = | |
{'0', '1', '2', '3', '4', '5', '6', '7', | |
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; | |
int arrayAllocator[64]; | |
int* ptrToConvertedNumber = &arrayAllocator; | |
long int numberToConvert; | |
int base, index = 0; | |
/* get the number and base */ | |
printf("Enter number and desired base: "); | |
scanf("%ld %i", &numberToConvert, &base); | |
/* convert to the indicated base */ | |
while (numberToConvert != 0) | |
{ | |
*(ptrToConvertedNumber + index) = numberToConvert % base; | |
numberToConvert = numberToConvert / base; | |
++index; | |
} | |
/* now print the result in reverse order */ | |
--index; /* back up to last entry in the array */ | |
printf("\n\nConverted Number = "); | |
for( ; index>=0; index--) /* go backward through array */ | |
{ | |
printf("%c", base_digits[*(ptrToConvertedNumber + index)]); | |
} | |
printf("\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment