I claim that this function is not accurate.
Here's the source code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void getBase(int num, int base, char* string);
char numbers[] = {'0', '1', '2', '3',
'4', '5', '6', '7',
'8', '9', 'A', 'B',
'C', 'D', 'E', 'F'};
int
main (int argc, char *argv[])
{
char str[32];
getBase(2, 2, str);
printf("%s\n", str);
getBase(255, 16, str);
printf("%s\n", str);
getBase(769878, 10, str);
printf("%s\n", str);
return 0;
}
void getBase(int n, int b, char* str)
{
const size_t SIZE = 32;
char arr[32+1]={0}; int digits=SIZE, i;
char* ptr = arr;
while (n > 0)
{
int t = n%b;
n/=b;
arr[--digits] = numbers[t];
}
while ( *ptr == '\0') ptr++;
strcpy(str, ptr);
}
No comments:
Post a Comment