Skip to content

Instantly share code, notes, and snippets.

@twof
Created September 8, 2016 04:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twof/f66779b16f50de7f537f1b55303380e1 to your computer and use it in GitHub Desktop.
Save twof/f66779b16f50de7f537f1b55303380e1 to your computer and use it in GitHub Desktop.
int main(void)
{
char base_digits[16] =
{'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
int convertedNumber;
int* ptrToConvertedNumber = &convertedNumber;
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