Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.12 KB | None | 0 0
  1. /* ************************************************************************** */
  2. /*                                                                            */
  3. /*                                                        :::      ::::::::   */
  4. /*   ft_convert_base.c                                  :+:      :+:    :+:   */
  5. /*                                                    +:+ +:+         +:+     */
  6. /*   By: elajouan <marvin@42.fr>                    +#+  +:+       +#+        */
  7. /*                                                +#+#+#+#+#+   +#+           */
  8. /*   Created: 2019/08/20 23:11:23 by elajouan          #+#    #+#             */
  9. /*   Updated: 2019/08/21 10:25:26 by elajouan         ###   ########.fr       */
  10. /*                                                                            */
  11. /* ************************************************************************** */
  12.  
  13. #include <stdlib.h>
  14.  
  15. char    *ft_itoa_base(int value, char *base);
  16. char    *ft_convert_base(char *nbr, char *base_from, char *base_to);
  17.  
  18. int     position_char_base(char c, char *base)
  19. {
  20.     int i;
  21.  
  22.     i = 0;
  23.     while (base[i])
  24.     {
  25.         if (base[i] == c)
  26.             return (i);
  27.         i++;
  28.     }
  29.     return (-1);
  30. }
  31.  
  32. int     ft_sign(char c, int *sign)
  33. {
  34.     if (c == '-')
  35.         return (*sign *= -1);
  36.     else if (c == '+')
  37.         return (1);
  38.     else
  39.         return (0);
  40. }
  41.  
  42. int     ft_blank(char c)
  43. {
  44.     if (c <= 32)
  45.         return (1);
  46.     return (0);
  47. }
  48.  
  49. int     check_base(char *base)
  50. {
  51.     int i;
  52.     int j;
  53.  
  54.     i = 0;
  55.     j = 0;
  56.     if ((base[0] == '\0') || (base[1] == '\0'))
  57.         return (0);
  58.     while (base[i])
  59.     {
  60.         j = i + 1;
  61.         if (base[i] == ' ' || base[i] == '+' || base[i] == '-'
  62.                 || base[i] == '\t' || base[i] == '\n' || base[i] == '\v'
  63.                 || base[i] == '\f' || base[i] == '\r'
  64.                 || (base[i] <= 32 && base[i] > 126))
  65.             return (0);
  66.         while (base[j])
  67.         {
  68.             if (base[i] == base[j])
  69.                 return (0);
  70.             j++;
  71.         }
  72.         i++;
  73.     }
  74.     return (i);
  75. }
  76.  
  77. int     ft_atoi_base(char *str, char *base)
  78. {
  79.     int res;
  80.     int sign;
  81.     int size_base;
  82.  
  83.     res = 0;
  84.     sign = 1;
  85.     size_base = check_base(base);
  86.     while (ft_blank(*str))
  87.         str++;
  88.     while (ft_sign(*str, &sign))
  89.         str++;
  90.     while (position_char_base(*str, base) != -1)
  91.     {
  92.         res = size_base * (res + position_char_base(*str, base));
  93.         str++;
  94.     }
  95.     res = res / size_base;
  96.     return (res * sign);
  97. }
  98. /* ************************************************************************** */
  99. /*                                                                            */
  100. /*                                                        :::      ::::::::   */
  101. /*   ft_convert_base2.c                                 :+:      :+:    :+:   */
  102. /*                                                    +:+ +:+         +:+     */
  103. /*   By: elajouan <marvin@42.fr>                    +#+  +:+       +#+        */
  104. /*                                                +#+#+#+#+#+   +#+           */
  105. /*   Created: 2019/08/20 23:12:05 by elajouan          #+#    #+#             */
  106. /*   Updated: 2019/08/21 21:20:08 by elajouan         ###   ########.fr       */
  107. /*                                                                            */
  108. /* ************************************************************************** */
  109.  
  110. #include <stdlib.h>
  111.  
  112. int     ft_atoi_base(char *str, char *base);
  113. int     check_base(char *base);
  114. int     ft_blank(char c);
  115. int     position_char_base(char c, char *base);
  116. int     ft_sign(char c, int *sign);
  117.  
  118. char    *ft_itoa_base(int value, char *base)
  119. {
  120.     unsigned int    nb;
  121.     unsigned int    nb2;
  122.     int             size_base;
  123.     char            *string;
  124.     int             i;
  125.  
  126.     size_base = check_base(base);
  127.     nb = (value < 0) ? -value : value;
  128.     nb2 = (value < 0) ? -value : value;
  129.     i = (value < 0) ? 2 : 1;
  130.     while ((nb = nb / size_base) >= 1)
  131.         i++;
  132.     string = (char*)malloc(sizeof(char) * (i + 1));
  133.     string[i] = '\0';
  134.     while (i--)
  135.     {
  136.         string[i] = base[nb2 % size_base];
  137.         nb2 = nb2 / size_base;
  138.     }
  139.     (value < 0) ? string[i + 1] = '-' : 0;
  140.     return (string);
  141. }
  142.  
  143. char    *ft_convert_base(char *nbr, char *base_from, char *base_to)
  144. {
  145.     int     value;
  146.     char    *res;
  147.  
  148.     if ((!check_base(base_from) || !check_base(base_to)))
  149.         return (NULL);
  150.     value = ft_atoi_base(nbr, base_from);
  151.     res = ft_itoa_base(value, base_to);
  152.     return (res);
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement