added itoa, lltoa, uitoa, ulltoa functions
This commit is contained in:
parent
2696f69d10
commit
070e9fae98
4 changed files with 165 additions and 0 deletions
45
libftasm/ft_itoa_base.c
Normal file
45
libftasm/ft_itoa_base.c
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
#include "libft.h"
|
||||
|
||||
static size_t ft_size(int n, int base)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 1;
|
||||
while (n /= base)
|
||||
i++;
|
||||
return (i);
|
||||
}
|
||||
|
||||
char *ft_itoa_base(int nbr, char *base, char *flags)
|
||||
{
|
||||
int i;
|
||||
int neg;
|
||||
int base_size;
|
||||
char *str;
|
||||
|
||||
i = 0;
|
||||
base_size = ft_strlen(base);
|
||||
str = ft_strnew(ft_size(nbr, base_size) + 1);
|
||||
neg = 0;
|
||||
if (nbr < 0)
|
||||
neg = 1;
|
||||
if (nbr == 0)
|
||||
{
|
||||
str[i++] = '0';
|
||||
str[i] = '\0';
|
||||
return (str);
|
||||
}
|
||||
while (nbr)
|
||||
{
|
||||
str[i++] = base[FT_ABS(nbr % base_size)];
|
||||
nbr = nbr / base_size;
|
||||
}
|
||||
if (neg)
|
||||
str[i++] = '-';
|
||||
else if (ft_strchr(flags, '+'))
|
||||
str[i++] = '+';
|
||||
else if (ft_strchr(flags, ' '))
|
||||
str[i++] = ' ';
|
||||
str[i] = '\0';
|
||||
return (ft_strrev(str));
|
||||
}
|
||||
45
libftasm/ft_lltoa_base.c
Normal file
45
libftasm/ft_lltoa_base.c
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
#include "libft.h"
|
||||
|
||||
static size_t ft_size(long long n, int base)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 1;
|
||||
while (n /= base)
|
||||
i++;
|
||||
return (i);
|
||||
}
|
||||
|
||||
char *ft_lltoa_base(long long nbr, char *base, char *flags)
|
||||
{
|
||||
int i;
|
||||
int neg;
|
||||
int base_size;
|
||||
char *str;
|
||||
|
||||
i = 0;
|
||||
base_size = ft_strlen(base);
|
||||
str = ft_strnew(ft_size(nbr, base_size) + 1);
|
||||
neg = 0;
|
||||
if (nbr < 0)
|
||||
neg = 1;
|
||||
if (nbr == 0)
|
||||
{
|
||||
str[i++] = '0';
|
||||
str[i] = '\0';
|
||||
return (str);
|
||||
}
|
||||
while (nbr)
|
||||
{
|
||||
str[i++] = base[FT_ABS(nbr % base_size)];
|
||||
nbr = nbr / base_size;
|
||||
}
|
||||
if (neg)
|
||||
str[i++] = '-';
|
||||
else if (ft_strchr(flags, '+'))
|
||||
str[i++] = '+';
|
||||
else if (ft_strchr(flags, ' '))
|
||||
str[i++] = ' ';
|
||||
str[i] = '\0';
|
||||
return (ft_strrev(str));
|
||||
}
|
||||
37
libftasm/ft_uitoa_base.c
Normal file
37
libftasm/ft_uitoa_base.c
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
#include "libft.h"
|
||||
|
||||
static size_t ft_size(unsigned int n, int base)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 1;
|
||||
while (n /= base)
|
||||
i++;
|
||||
return (i);
|
||||
}
|
||||
|
||||
char *ft_uitoa_base(unsigned int nbr, char *base)
|
||||
{
|
||||
int i;
|
||||
int base_size;
|
||||
char *str;
|
||||
|
||||
/* printf("converting %llu to base %s\n", nbr, base); */
|
||||
/* fflush(stdout); */
|
||||
i = 0;
|
||||
base_size = ft_strlen(base);
|
||||
str = ft_strnew(ft_size(nbr, base_size) + 1);
|
||||
if (nbr == 0)
|
||||
{
|
||||
str[i++] = '0';
|
||||
str[i] = '\0';
|
||||
return (str);
|
||||
}
|
||||
while (nbr)
|
||||
{
|
||||
str[i++] = base[nbr % base_size];
|
||||
nbr = nbr / base_size;
|
||||
}
|
||||
str[i] = '\0';
|
||||
return (ft_strrev(str));
|
||||
}
|
||||
38
libftasm/ft_ulltoa_base.c
Normal file
38
libftasm/ft_ulltoa_base.c
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
#include "libft.h"
|
||||
|
||||
static size_t ft_size(unsigned long long n, int base)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 1;
|
||||
while (n /= base)
|
||||
i++;
|
||||
return (i);
|
||||
}
|
||||
|
||||
char *ft_ulltoa_base(unsigned long long nbr, char *base)
|
||||
{
|
||||
int i;
|
||||
int base_size;
|
||||
char *str;
|
||||
|
||||
/* printf("converting %llu to base %s\n", nbr, base); */
|
||||
/* fflush(stdout); */
|
||||
i = 0;
|
||||
base_size = ft_strlen(base);
|
||||
str = ft_strnew(ft_size(nbr, base_size) + 1);
|
||||
if (nbr == 0)
|
||||
{
|
||||
str[i++] = '0';
|
||||
str[i] = '\0';
|
||||
return (str);
|
||||
}
|
||||
while (nbr)
|
||||
{
|
||||
str[i++] = base[nbr % base_size];
|
||||
nbr = nbr / base_size;
|
||||
}
|
||||
str[i] = '\0';
|
||||
return (ft_strrev(str));
|
||||
}
|
||||
|
||||
Loading…
Reference in a new issue