Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_convert_base.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: elajouan <marvin@42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2019/08/20 23:11:23 by elajouan #+# #+# */
- /* Updated: 2019/08/21 10:25:26 by elajouan ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include <stdlib.h>
- char *ft_itoa_base(int value, char *base);
- char *ft_convert_base(char *nbr, char *base_from, char *base_to);
- int position_char_base(char c, char *base)
- {
- int i;
- i = 0;
- while (base[i])
- {
- if (base[i] == c)
- return (i);
- i++;
- }
- return (-1);
- }
- int ft_sign(char c, int *sign)
- {
- if (c == '-')
- return (*sign *= -1);
- else if (c == '+')
- return (1);
- else
- return (0);
- }
- int ft_blank(char c)
- {
- if (c <= 32)
- return (1);
- return (0);
- }
- int check_base(char *base)
- {
- int i;
- int j;
- i = 0;
- j = 0;
- if ((base[0] == '\0') || (base[1] == '\0'))
- return (0);
- while (base[i])
- {
- j = i + 1;
- if (base[i] == ' ' || base[i] == '+' || base[i] == '-'
- || base[i] == '\t' || base[i] == '\n' || base[i] == '\v'
- || base[i] == '\f' || base[i] == '\r'
- || (base[i] <= 32 && base[i] > 126))
- return (0);
- while (base[j])
- {
- if (base[i] == base[j])
- return (0);
- j++;
- }
- i++;
- }
- return (i);
- }
- int ft_atoi_base(char *str, char *base)
- {
- int res;
- int sign;
- int size_base;
- res = 0;
- sign = 1;
- size_base = check_base(base);
- while (ft_blank(*str))
- str++;
- while (ft_sign(*str, &sign))
- str++;
- while (position_char_base(*str, base) != -1)
- {
- res = size_base * (res + position_char_base(*str, base));
- str++;
- }
- res = res / size_base;
- return (res * sign);
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_convert_base2.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: elajouan <marvin@42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2019/08/20 23:12:05 by elajouan #+# #+# */
- /* Updated: 2019/08/21 21:20:08 by elajouan ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include <stdlib.h>
- int ft_atoi_base(char *str, char *base);
- int check_base(char *base);
- int ft_blank(char c);
- int position_char_base(char c, char *base);
- int ft_sign(char c, int *sign);
- char *ft_itoa_base(int value, char *base)
- {
- unsigned int nb;
- unsigned int nb2;
- int size_base;
- char *string;
- int i;
- size_base = check_base(base);
- nb = (value < 0) ? -value : value;
- nb2 = (value < 0) ? -value : value;
- i = (value < 0) ? 2 : 1;
- while ((nb = nb / size_base) >= 1)
- i++;
- string = (char*)malloc(sizeof(char) * (i + 1));
- string[i] = '\0';
- while (i--)
- {
- string[i] = base[nb2 % size_base];
- nb2 = nb2 / size_base;
- }
- (value < 0) ? string[i + 1] = '-' : 0;
- return (string);
- }
- char *ft_convert_base(char *nbr, char *base_from, char *base_to)
- {
- int value;
- char *res;
- if ((!check_base(base_from) || !check_base(base_to)))
- return (NULL);
- value = ft_atoi_base(nbr, base_from);
- res = ft_itoa_base(value, base_to);
- return (res);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement