ft_convert_base{,2}.c
This commit is contained in:
parent
1bccb37c68
commit
940715a153
|
@ -1,179 +1,420 @@
|
|||
#include <unistd.h>
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_convert_base.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tosuman </var/spool/mail/tosuman> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/30 22:35:01 by tosuman #+# #+# */
|
||||
/* Updated: 2023/03/30 22:35:02 by tosuman ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <limits.h>
|
||||
#include <stdlib.h>
|
||||
#define MAX_OUTPUT_LEN 100
|
||||
|
||||
unsigned int char_count(const char *str, char c)
|
||||
char *ft_strcat(char *dest, char *src);
|
||||
int char_index(const char *str, const char c);
|
||||
unsigned int char_count(const char *str, char c);
|
||||
unsigned int is_valid_base(char *base, int space_ok);
|
||||
|
||||
void strcat_base_digit(unsigned int digit_idx, char *base, char *result)
|
||||
{
|
||||
unsigned int count;
|
||||
char digit[2];
|
||||
|
||||
count = 0;
|
||||
while (*str)
|
||||
{
|
||||
if (*str == c)
|
||||
++count;
|
||||
++str;
|
||||
}
|
||||
return (count);
|
||||
digit[0] = base[digit_idx];
|
||||
digit[1] = 0;
|
||||
ft_strcat(result, digit);
|
||||
}
|
||||
|
||||
// Return 0 if the base is invalid or the length of the base otherwise
|
||||
unsigned int is_valid_base(const char *base, int space_ok)
|
||||
void _ft_to_base(int nbr, char *base, unsigned int base_len, char *result)
|
||||
{
|
||||
unsigned int size;
|
||||
char digit_idx;
|
||||
|
||||
size = 0;
|
||||
while (*base)
|
||||
{
|
||||
if (*base == '+' || *base == '-' || (*base == ' ' && !space_ok))
|
||||
return (0);
|
||||
if (char_count(base, *base) > 1)
|
||||
return (0);
|
||||
++size;
|
||||
++base;
|
||||
}
|
||||
if (size < 2)
|
||||
return (0);
|
||||
return (size);
|
||||
digit_idx = nbr % base_len;
|
||||
if (nbr > (int) base_len - 1)
|
||||
{
|
||||
_ft_to_base(nbr / base_len, base, base_len, result);
|
||||
strcat_base_digit(digit_idx, base, result);
|
||||
}
|
||||
else if (nbr == INT_MIN)
|
||||
{
|
||||
_ft_to_base(-(nbr / base_len), base, base_len, result);
|
||||
strcat_base_digit(digit_idx, base, result);
|
||||
}
|
||||
else if (nbr < 0)
|
||||
{
|
||||
ft_strcat(result, "-");
|
||||
_ft_to_base(-nbr, base, base_len, result);
|
||||
}
|
||||
else
|
||||
strcat_base_digit(digit_idx, base, result);
|
||||
}
|
||||
|
||||
void putdigit_from_base(unsigned int digit_idx, const char *base)
|
||||
char *ft_to_base(int nbr, char *base_to)
|
||||
{
|
||||
write(1, &base[digit_idx], 1);
|
||||
unsigned int base_len;
|
||||
char *result;
|
||||
int idx;
|
||||
|
||||
base_len = is_valid_base(base_to, 1);
|
||||
if (!base_len)
|
||||
return ((void *) 0);
|
||||
result = malloc(sizeof(char) * MAX_OUTPUT_LEN);
|
||||
idx = -1;
|
||||
while (++idx < MAX_OUTPUT_LEN)
|
||||
result[idx] = 0;
|
||||
_ft_to_base(nbr, base_to, base_len, result);
|
||||
return (result);
|
||||
}
|
||||
|
||||
void _ft_putnbr_base(int nbr, const char *base, unsigned int base_len)
|
||||
int ft_atoi_base(char *str, char *base)
|
||||
{
|
||||
char digit_idx;
|
||||
int sign;
|
||||
int abs;
|
||||
int index_in_base;
|
||||
unsigned int base_len;
|
||||
|
||||
// unsigned int base_len will make signed nbr positive
|
||||
digit_idx = nbr % base_len;
|
||||
if (nbr > (int) base_len - 1)
|
||||
{
|
||||
_ft_putnbr_base(nbr / base_len, base, base_len);
|
||||
putdigit_from_base(digit_idx, base);
|
||||
}
|
||||
else if (nbr == INT_MIN)
|
||||
{
|
||||
_ft_putnbr_base(-(nbr / base_len), base, base_len);
|
||||
putdigit_from_base(digit_idx, base);
|
||||
}
|
||||
else if (nbr < 0)
|
||||
{
|
||||
write(1, "-", 1);
|
||||
_ft_putnbr_base(-nbr, base, base_len);
|
||||
}
|
||||
else
|
||||
putdigit_from_base(digit_idx, base);
|
||||
}
|
||||
|
||||
int max_result_len(char *nbr, char *base_from, char *base_to)
|
||||
{
|
||||
return (100);
|
||||
}
|
||||
|
||||
char *ft_to_base(int nbr, char *base_from, char *base_to)
|
||||
{
|
||||
unsigned int base_len;
|
||||
char *result;
|
||||
|
||||
base_len = is_valid_base(base_to, 1);
|
||||
if (!base_len)
|
||||
return ((void *) 0);
|
||||
result = malloc(sizeof(char) * max_result_len(nbr, base_from, base_to));
|
||||
_ft_putnbr_base(nbr, base_to, base_len, result);
|
||||
return (result);
|
||||
}
|
||||
|
||||
int char_index(const char *str, const char c)
|
||||
{
|
||||
int idx;
|
||||
|
||||
idx = 0;
|
||||
while (*str)
|
||||
{
|
||||
if (*str++ == c)
|
||||
return (idx);
|
||||
++idx;
|
||||
}
|
||||
return (-1);
|
||||
}
|
||||
|
||||
int ft_atoi_base(char *str, char *base)
|
||||
{
|
||||
int sign;
|
||||
int abs;
|
||||
int index_in_base;
|
||||
unsigned int base_len;
|
||||
|
||||
abs = 0;
|
||||
sign = 1;
|
||||
base_len = is_valid_base(base, 0);
|
||||
if (!base_len)
|
||||
return (0);
|
||||
while (*str == ' '
|
||||
|| *str == '\t'
|
||||
|| *str == '\n'
|
||||
|| *str == '\r'
|
||||
|| *str == '\f'
|
||||
|| *str == '\v')
|
||||
++str;
|
||||
while (*str == '+'
|
||||
|| *str == '-')
|
||||
if (*str++ == '-')
|
||||
sign *= -1;
|
||||
index_in_base = char_index(base, *str);
|
||||
while (index_in_base >= 0)
|
||||
{
|
||||
abs = abs * base_len + index_in_base;
|
||||
++str;
|
||||
index_in_base = char_index(base, *str);
|
||||
}
|
||||
return (sign * abs);
|
||||
abs = 0;
|
||||
sign = 1;
|
||||
base_len = is_valid_base(base, 0);
|
||||
if (!base_len)
|
||||
return (0);
|
||||
while (*str == ' ' || *str == '\t'
|
||||
|| *str == '\n' || *str == '\r'
|
||||
|| *str == '\f' || *str == '\v')
|
||||
++str;
|
||||
while (*str == '+' || *str == '-')
|
||||
if (*str++ == '-')
|
||||
sign *= -1;
|
||||
index_in_base = char_index(base, *str);
|
||||
while (index_in_base >= 0)
|
||||
{
|
||||
abs = abs * base_len + index_in_base;
|
||||
++str;
|
||||
index_in_base = char_index(base, *str);
|
||||
}
|
||||
return (sign * abs);
|
||||
}
|
||||
|
||||
char *ft_convert_base(char *nbr, char *base_from, char *base_to)
|
||||
{
|
||||
return (ft_to_base(ft_atoi_base(nbr, base_from), base_from, base_to));
|
||||
return (ft_to_base(ft_atoi_base(nbr, base_from), base_to));
|
||||
}
|
||||
|
||||
/* ////
|
||||
#include <stdio.h>
|
||||
#define INVALID_FROM_BASE_1 "01234567890"
|
||||
#define INVALID_TO_BASE_1 "010"
|
||||
#define INVALID_BASE_1 "01234567890"
|
||||
#define INVALID_BASE_2 "010"
|
||||
#define INVALID_BASE_3 "0123456789 "
|
||||
#define INVALID_BASE_4 "01 "
|
||||
|
||||
#define INVALID_FROM_BASE_2 "010"
|
||||
#define INVALID_TO_BASE_2 "01234567890"
|
||||
|
||||
#define FROM_BASE_1 "0123456789"
|
||||
#define TO_BASE_1 "0123456789"
|
||||
|
||||
#define FROM_BASE_2 "0123456789"
|
||||
#define TO_BASE_2 "01"
|
||||
|
||||
#define FROM_BASE_3 "01"
|
||||
#define TO_BASE_3 "0123456789"
|
||||
|
||||
#define FROM_BASE_4 "0123456789"
|
||||
#define TO_BASE_4 "0123456789abcdef"
|
||||
|
||||
#define FROM_BASE_5 "01"
|
||||
#define TO_BASE_5 "0123456789abcdef"
|
||||
|
||||
#define FROM_BASE_6 "0123456789abcdef"
|
||||
#define TO_BASE_6 "01"
|
||||
#define BASE10 "0123456789"
|
||||
#define BASE2 "01"
|
||||
#define BASE16 "0123456789abcdef"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char *from_base;
|
||||
char *to_base;
|
||||
char *output;
|
||||
char *nbr;
|
||||
char *from_base;
|
||||
char *to_base;
|
||||
char *output;
|
||||
char *nbr;
|
||||
|
||||
from_base = FROM_BASE_2;
|
||||
to_base = TO_BASE_2;
|
||||
printf("FROM BASE: %s, TO BASE: %s\n", from_base, to_base);
|
||||
// nbr = "-2"; printf("Input: <%s>, Output: <%s>\n", nbr, ft_convert_base(nbr, from_base, to_base));
|
||||
// nbr = "-1"; printf("Input: <%s>, Output: <%s>\n", nbr, ft_convert_base(nbr, from_base, to_base));
|
||||
nbr = "0"; 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 = 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");
|
||||
|
||||
return (0);
|
||||
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);
|
||||
}
|
||||
*/ ////
|
||||
|
|
|
@ -0,0 +1,72 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_convert_base2.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tosuman </var/spool/mail/tosuman> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/30 22:43:16 by tosuman #+# #+# */
|
||||
/* Updated: 2023/03/30 22:43:17 by tosuman ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
char *ft_strcat(char *dest, char *src)
|
||||
{
|
||||
char *orig_s1;
|
||||
|
||||
orig_s1 = dest;
|
||||
while (*dest)
|
||||
++dest;
|
||||
while (*src)
|
||||
*dest++ = *src++;
|
||||
*dest = 0;
|
||||
return (orig_s1);
|
||||
}
|
||||
|
||||
unsigned int char_count(const char *str, char c)
|
||||
{
|
||||
unsigned int count;
|
||||
|
||||
count = 0;
|
||||
while (*str)
|
||||
{
|
||||
if (*str == c)
|
||||
++count;
|
||||
++str;
|
||||
}
|
||||
return (count);
|
||||
}
|
||||
|
||||
// Return 0 if the base is invalid or the length of the base otherwise
|
||||
unsigned int is_valid_base(char *base, int space_ok)
|
||||
{
|
||||
unsigned int size;
|
||||
|
||||
size = 0;
|
||||
while (*base)
|
||||
{
|
||||
if (*base == '+' || *base == '-' || (*base == ' ' && !space_ok))
|
||||
return (0);
|
||||
if (char_count(base, *base) > 1)
|
||||
return (0);
|
||||
++size;
|
||||
++base;
|
||||
}
|
||||
if (size < 2)
|
||||
return (0);
|
||||
return (size);
|
||||
}
|
||||
|
||||
int char_index(const char *str, const char c)
|
||||
{
|
||||
int idx;
|
||||
|
||||
idx = 0;
|
||||
while (*str)
|
||||
{
|
||||
if (*str++ == c)
|
||||
return (idx);
|
||||
++idx;
|
||||
}
|
||||
return (-1);
|
||||
}
|
Loading…
Reference in New Issue