ft_itohex.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. /* ************************************************************************** */
  2. /* */
  3. /* ::: :::::::: */
  4. /* ft_itohex.c :+: :+: :+: */
  5. /* +:+ +:+ +:+ */
  6. /* By: bchanot <bchanot@student.42.fr> +#+ +:+ +#+ */
  7. /* +#+#+#+#+#+ +#+ */
  8. /* Created: 2018/01/16 00:21:29 by bchanot #+# #+# */
  9. /* Updated: 2018/01/16 00:34:09 by xuser ### ########.fr */
  10. /* */
  11. /* ************************************************************************** */
  12. #include "libft.h"
  13. static void ft_itohex_loop(unsigned long nb, char **str, int depth)
  14. {
  15. const char *hex = "0123456789abcdef";
  16. unsigned long index;
  17. index = nb % 16;
  18. if (nb / 16 > 0)
  19. ft_itohex_loop(nb / 16, str, (depth - 1));
  20. (*str)[depth] = hex[index];
  21. }
  22. char *ft_itohex(unsigned long nb, int depth)
  23. {
  24. char *str;
  25. int cpt;
  26. str = ft_strnew(depth + 1);
  27. cpt = 0;
  28. while (cpt <= depth)
  29. str[cpt++] = '0';
  30. ft_itohex_loop(nb, &str, depth);
  31. return (str);
  32. }