ft_utoa.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /* ************************************************************************** */
  2. /* */
  3. /* ::: :::::::: */
  4. /* ft_utoa.c :+: :+: :+: */
  5. /* +:+ +:+ +:+ */
  6. /* By: bchanot <bchanot@students.42.fr> +#+ +:+ +#+ */
  7. /* +#+#+#+#+#+ +#+ */
  8. /* Created: 2016/02/11 19:54:05 by bchanot #+# #+# */
  9. /* Updated: 2018/04/17 11:52:53 by bchanot ### ########.fr */
  10. /* */
  11. /* ************************************************************************** */
  12. #include "libft.h"
  13. char *ft_utoa(long long unsigned int nb)
  14. {
  15. long long unsigned int x;
  16. char *str;
  17. int cpt;
  18. x = nb;
  19. cpt = 0;
  20. while (x > 10)
  21. {
  22. x /= 10;
  23. cpt++;
  24. }
  25. if ((str = (char *)ft_memalloc(sizeof(char *) * cpt + 1)))
  26. {
  27. str[cpt + 1] = '\0';
  28. while (cpt >= 0)
  29. {
  30. x = nb % 10;
  31. str[cpt] = 48 + x;
  32. nb /= 10;
  33. cpt--;
  34. }
  35. }
  36. return (str);
  37. }