pf_putnbr.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* ************************************************************************** */
  2. /* */
  3. /* ::: :::::::: */
  4. /* ft_putnbr.c :+: :+: :+: */
  5. /* +:+ +:+ +:+ */
  6. /* By: bchanot <bchanot@students.42.fr> +#+ +:+ +#+ */
  7. /* +#+#+#+#+#+ +#+ */
  8. /* Created: 2016/03/03 02:08:12 by bchanot #+# #+# */
  9. /* Updated: 2017/04/15 00:41:32 by bchanot ### ########.fr */
  10. /* */
  11. /* ************************************************************************** */
  12. #include "libftprintf.h"
  13. #include <stdlib.h>
  14. int pf_puthex(unsigned int n, int fd)
  15. {
  16. char *hex;
  17. hex = NULL;
  18. if (n >= 16)
  19. return (pf_puthex(n / 16, fd) + pf_puthex(n % 16, fd));
  20. else
  21. {
  22. hex = ft_strdup("0123456789abcdef");
  23. ft_putchar_fd(hex[n], fd);
  24. if (hex)
  25. free(hex);
  26. return (1);
  27. }
  28. }
  29. int pf_putoctal(unsigned int n, int fd)
  30. {
  31. if (n >= 8)
  32. return (pf_putoctal(n / 8, fd) + pf_puthex(n % 8, fd));
  33. else
  34. {
  35. ft_putchar_fd(n + 48, fd);
  36. return (1);
  37. }
  38. }
  39. int pf_puthexm(unsigned int n, int fd)
  40. {
  41. char *hex;
  42. hex = NULL;
  43. if (n >= 16)
  44. return (pf_puthexm(n / 16, fd) + pf_puthexm(n % 16, fd));
  45. else
  46. {
  47. hex = ft_strdup("0123456789ABCDEF");
  48. ft_putchar_fd(hex[n], fd);
  49. if (hex)
  50. free(hex);
  51. return (1);
  52. }
  53. }
  54. int pf_putunbr(unsigned int n, int fd)
  55. {
  56. if (n >= 10)
  57. return (pf_putunbr(n / 10, fd) + pf_putunbr(n % 10, fd));
  58. ft_putchar_fd(48 + n, fd);
  59. return (1);
  60. }