diff --git a/ft_parse.c b/ft_parse.c index 7d3c32e..9486652 100644 --- a/ft_parse.c +++ b/ft_parse.c @@ -6,7 +6,7 @@ /* By: dsemenov +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/28 15:56:14 by dsemenov #+# #+# */ -/* Updated: 2024/12/03 16:23:56 by dsemenov ### ########.fr */ +/* Updated: 2024/12/03 19:10:37 by dsemenov ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,5 +32,12 @@ int ft_parse(va_list ap, const char c) len += ft_putnbr(va_arg(ap, int)); else if (c == 'u') len += ft_unsigned_putnbr(va_arg(ap, unsigned int)); + else if (c == 'x' || c == 'X') + if (c == 'x') + { + len += ft_putchar('0'); + len += ft_putchar('x'); + len += ft_puthex(va_arg(ap, int)); + } return (len); } diff --git a/ft_printf.c b/ft_printf.c index db5cded..102c393 100644 --- a/ft_printf.c +++ b/ft_printf.c @@ -6,7 +6,7 @@ /* By: dsemenov +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/25 17:49:25 by dsemenov #+# #+# */ -/* Updated: 2024/12/03 17:54:02 by dsemenov ### ########.fr */ +/* Updated: 2024/12/03 19:11:22 by dsemenov ### ########.fr */ /* */ /* ************************************************************************** */ @@ -47,8 +47,8 @@ int ft_printf(const char *format, ...) /* int main(void) { - __builtin_printf("%d\n", ft_printf("%u", 42949672)); - __builtin_printf("%d\n", __builtin_printf("%u", 42949672)); + __builtin_printf("%d\n", ft_printf("%x", 42949672)); + __builtin_printf("%d\n", __builtin_printf("%x", 42949672)); } */ diff --git a/ft_printf.h b/ft_printf.h index 0be3dff..1b7df1d 100644 --- a/ft_printf.h +++ b/ft_printf.h @@ -6,7 +6,7 @@ /* By: dsemenov +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/29 14:24:31 by dsemenov #+# #+# */ -/* Updated: 2024/12/03 17:40:20 by dsemenov ### ########.fr */ +/* Updated: 2024/12/03 19:10:19 by dsemenov ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,5 +23,6 @@ size_t num_len(int n); size_t ft_putnbr(int n); size_t ft_unsigned_putnbr(unsigned int n); int ft_parse(va_list ap, const char c); +size_t ft_puthex(unsigned int nbr); #endif diff --git a/ft_printf_utils.c b/ft_printf_utils.c index dce9331..c2945f2 100644 --- a/ft_printf_utils.c +++ b/ft_printf_utils.c @@ -6,7 +6,7 @@ /* By: dsemenov +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/28 16:49:00 by dsemenov #+# #+# */ -/* Updated: 2024/12/03 17:57:34 by dsemenov ### ########.fr */ +/* Updated: 2024/12/03 19:10:55 by dsemenov ### ########.fr */ /* */ /* ************************************************************************** */ @@ -81,3 +81,15 @@ size_t ft_unsigned_putnbr(unsigned int n) len += ft_putchar(n % 10 + '0'); return (len); } + +size_t ft_puthex(unsigned int nbr) +{ + size_t len; + char base[16] = "0123456789ABCDEF"; + + len = 0; + if (nbr >= 16) + len += ft_puthex(nbr / 16); + len += ft_putchar(base[nbr % 16]); + return (len); +}