314 lines
10 KiB
C
314 lines
10 KiB
C
#include <stdio.h>
|
|
#define INVALID_BASE_1 "01234567890"
|
|
#define INVALID_BASE_2 "010"
|
|
#define INVALID_BASE_3 "0123456789 "
|
|
#define INVALID_BASE_4 "01 "
|
|
|
|
#define BASE10 "0123456789"
|
|
#define BASE2 "01"
|
|
#define BASE16 "0123456789abcdef"
|
|
|
|
int main(void)
|
|
{
|
|
char *from_base;
|
|
char *to_base;
|
|
char *output;
|
|
char *nbr;
|
|
|
|
from_base = BASE10;
|
|
to_base = BASE10;
|
|
printf("FROM BASE: '%s', TO BASE: '%s'\n", from_base, to_base);
|
|
nbr = "-2147483648";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "-2147483647";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "-2";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "-1";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "0";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "1";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "2";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "3";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "4";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "5";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "2147483647";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
printf("\n");
|
|
|
|
from_base = BASE10;
|
|
to_base = BASE2;
|
|
printf("FROM BASE: '%s', TO BASE: '%s'\n", from_base, to_base);
|
|
nbr = "-2147483648";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "-2147483647";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "-2";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "-1";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "0";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "1";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "2";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "3";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "4";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "5";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "2147483647";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
printf("\n");
|
|
|
|
from_base = BASE2;
|
|
to_base = BASE10;
|
|
printf("FROM BASE: '%s', TO BASE: '%s'\n", from_base, to_base);
|
|
nbr = "-10000000000000000000000000000000";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "-1111111111111111111111111111111";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "-10";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "-1";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "0";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "1";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "10";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "11";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "100";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "101";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "1111111111111111111111111111111";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
printf("\n");
|
|
|
|
from_base = BASE2;
|
|
to_base = BASE16;
|
|
printf("FROM BASE: '%s', TO BASE: '%s'\n", from_base, to_base);
|
|
nbr = "-10000000000000000000000000000000";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "-1111111111111111111111111111111";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "-10";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "-1";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "0";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "1";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "10";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "11";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "100";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "101";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "1111111111111111111111111111111";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
printf("\n");
|
|
|
|
from_base = BASE16;
|
|
to_base = BASE2;
|
|
printf("FROM BASE: '%s', TO BASE: '%s'\n", from_base, to_base);
|
|
nbr = "-80000000";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "-7fffffff";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "-2";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "-1";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "0";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "1";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "2";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "3";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "4";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "5";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "7fffffff";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
printf("\n");
|
|
|
|
from_base = BASE10;
|
|
to_base = BASE16;
|
|
printf("FROM BASE: '%s', TO BASE: '%s'\n", from_base, to_base);
|
|
nbr = "-2147483648";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "-2147483647";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "-2";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "-1";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "0";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "1";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "2";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "3";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "4";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "5";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
nbr = "2147483647";
|
|
output = ft_convert_base(nbr, from_base, to_base);
|
|
printf("Input: <%s>, Output: <%s>\n", nbr, output);
|
|
free(output);
|
|
printf("\n");
|
|
|
|
return (0);
|
|
}
|