Skip to content

Commit

Permalink
added ft_itoa_base
Browse files Browse the repository at this point in the history
  • Loading branch information
Anaelle LECLABART committed Jan 2, 2019
1 parent 7e84cd4 commit 07401fb
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# By: anaiel <[email protected]> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2018/11/06 17:19:55 by anleclab #+# #+# #
# Updated: 2019/01/02 10:38:38 by anleclab ### ########.fr #
# Updated: 2019/01/02 11:56:54 by anleclab ### ########.fr #
# #
# **************************************************************************** #

Expand All @@ -25,7 +25,7 @@ SRC = ft_memset.c ft_bzero.c ft_memcpy.c ft_memccpy.c ft_memmove.c ft_memchr.c \
ft_lstdelone.c ft_lstdel.c ft_lstadd.c ft_lstiter.c ft_lstmap.c \
ft_strsplitlst.c ft_putstrarray.c ft_putstrlst.c ft_putfile.c \
ft_filelinecount.c ft_putwchar.c ft_printbits.c ft_wchar_to_bytes.c \
ft_double_power.c ft_array_swap.c
ft_double_power.c ft_array_swap.c ft_itoa_base.c
OBJ = $(SRC:.c=.o)

.PHONY: all, clean, fclean, re
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Run the command `make` to create the **libft.a** library file.
### Conversions
- ft_atoi
- ft_itoa
- ft_itoa_base

### Printing
- ft_putchar
Expand All @@ -85,6 +86,7 @@ Run the command `make` to create the **libft.a** library file.
- ft_filelinecount
- ft_double_power
- ft_wchar_to_bytes
- ft_array_swap

## Updating

Expand Down
3 changes: 2 additions & 1 deletion libft.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: anaiel <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/06 17:19:32 by anleclab #+# #+# */
/* Updated: 2019/01/02 10:39:19 by anleclab ### ########.fr */
/* Updated: 2019/01/02 11:57:30 by anleclab ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -92,5 +92,6 @@ int ft_putwchar(wchar_t c);
unsigned char *ft_wchar_to_bytes(wint_t c);
double ft_double_power(double n, int pow);
void ft_array_swap(int *array, int i1, int i2);
char *ft_itoa_base(int n, int base);

#endif
70 changes: 70 additions & 0 deletions src/ft_itoa_base.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: anleclab <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/02 11:39:22 by anleclab #+# #+# */
/* Updated: 2019/01/02 11:55:06 by anleclab ### ########.fr */
/* */
/* ************************************************************************** */

#include <stdlib.h>
#include "../libft.h"

static char *init_base(int base)
{
char *charbase;
int i;

if (base < 2 || base > 16)
return (NULL);
if (!(charbase = ft_strnew(base + 1)))
return (NULL);
i = 0;
while (i < base && i < 10)
charbase[i] = i;
while (i < base)
charbase[i] = i - 10 + 'A';
return (charbase);
}

static int nb_digits(unsigned int nb, int base)
{
int nbdigits;

nbdigits = 0;
if (nb == 0)
return (1);
while (nb)
{
nbdigits++;
nb /= base;
}
return (nbdigits);
}

char *ft_itoa_base(int n, int base)
{
char *charbase;
char *res;
unsigned int nb;
int nbdigits;
int i;

if (!(charbase = init_base(base)))
return (NULL);
nb = (n < 0) ? -n : n;
nbdigits = nb_digits(nb, base);
if (!(res = ft_strnew(nbdigits + 1)))
return (NULL);
i = -1;
while (++i < nbdigits - 1)
{
res[nbdigits - 1 - i] = charbase[nb % base];
nb /= base;
}
free(charbase);
return (res);
}

0 comments on commit 07401fb

Please sign in to comment.