Skip to content

Commit

Permalink
Add logic for hex numbers. Not working yet
Browse files Browse the repository at this point in the history
  • Loading branch information
Extraden committed Dec 3, 2024
1 parent cdf4c46 commit fec8788
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
9 changes: 8 additions & 1 deletion ft_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: dsemenov <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand All @@ -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);
}
6 changes: 3 additions & 3 deletions ft_printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: dsemenov <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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));
}
*/
3 changes: 2 additions & 1 deletion ft_printf.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: dsemenov <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand All @@ -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
14 changes: 13 additions & 1 deletion ft_printf_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: dsemenov <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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);
}

0 comments on commit fec8788

Please sign in to comment.