summaryrefslogtreecommitdiff
path: root/src/ft_printhex.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ft_printhex.c')
-rw-r--r--src/ft_printhex.c51
1 files changed, 24 insertions, 27 deletions
diff --git a/src/ft_printhex.c b/src/ft_printhex.c
index e96b322..d65de1c 100644
--- a/src/ft_printhex.c
+++ b/src/ft_printhex.c
@@ -6,42 +6,39 @@
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/13 15:50:35 by dkaiser #+# #+# */
-/* Updated: 2024/03/13 16:00:38 by dkaiser ### ########.fr */
+/* Updated: 2024/03/15 12:40:38 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
#include "../ft_printf.h"
-static int get_len(unsigned int n)
+static void printhex_rec(unsigned int nbr, char fmt, int *len)
{
- int len;
+ char c;
+ int success;
- len = 0;
- if (n == 0)
- return 1;
- while (n)
- {
- len++;
- n /= 16;
- }
- return len;
+ if (*len < 0)
+ return ;
+ if (nbr % 16 < 10)
+ c = '0' + (nbr % 16);
+ else
+ c = (fmt - 33) + (nbr % 16);
+ if (nbr > 15)
+ printhex_rec(nbr / 16, fmt, len);
+ if (*len < 0)
+ return ;
+ success = write(1, &c, 1);
+ if (success < 0)
+ *len = -1;
+ else
+ (*len)++;
}
-static void printhex_rec(unsigned int nbr, char fmt)
+int ft_printhex(unsigned int nbr, char fmt)
{
- char c;
+ int len;
- if (nbr % 16 < 10)
- c = '0' + (nbr % 16);
- else
- c = fmt - 35 + (nbr % 16);
- if (nbr > 15)
- printhex_rec(nbr, fmt);
- write(1, &c, 1);
-}
-
-int ft_printhex(unsigned int nbr, char fmt)
-{
- printhex_rec(nbr, fmt);
- return get_len(nbr);
+ len = 0;
+ printhex_rec(nbr, fmt, &len);
+ return (len);
}