ft_htoa.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /* ************************************************************************** */
  2. /* */
  3. /* ::: :::::::: */
  4. /* ft_htoa.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:49:39 by bchanot ### ########.fr */
  10. /* */
  11. /* ************************************************************************** */
  12. #include "libft.h"
  13. static int valuehex(int x)
  14. {
  15. if (x >= 0 && x <= 9)
  16. return (48 + x);
  17. else if (x >= 10 && x <= 15)
  18. {
  19. x = x - 10;
  20. return (97 + x);
  21. }
  22. return (0);
  23. }
  24. char *ft_htoa(long long unsigned int nb)
  25. {
  26. long long unsigned int x;
  27. char *str;
  28. int cpt;
  29. x = nb;
  30. cpt = 0;
  31. while (x > 16)
  32. {
  33. x /= 16;
  34. cpt++;
  35. }
  36. if ((str = (char *)ft_memalloc(sizeof(char *) * cpt + 1)))
  37. {
  38. str[cpt + 1] = '\0';
  39. while (cpt >= 0)
  40. {
  41. x = nb % 16;
  42. str[cpt] = valuehex(x);
  43. nb /= 16;
  44. cpt--;
  45. }
  46. }
  47. return (str);
  48. }