summaryrefslogtreecommitdiff
path: root/libft/ft_printhex.c
diff options
context:
space:
mode:
authorDominik Kaiser2024-05-08 13:14:19 +0200
committerDominik Kaiser2024-05-08 13:14:19 +0200
commit95ccc46ad62c59e648679acad8b44ba5d4465e3d (patch)
treec053e4c692039d151cefa26759bee90d31d8c0a0 /libft/ft_printhex.c
parent42289aa55c7dafe8fffbcdfb0e02f089b7b83afb (diff)
downloadpipex-master.tar.gz
pipex-master.zip
Add libft as dirHEADmaster
Diffstat (limited to 'libft/ft_printhex.c')
-rw-r--r--libft/ft_printhex.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/libft/ft_printhex.c b/libft/ft_printhex.c
new file mode 100644
index 0000000..4605eac
--- /dev/null
+++ b/libft/ft_printhex.c
@@ -0,0 +1,44 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_printhex.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/03/13 15:50:35 by dkaiser #+# #+# */
+/* Updated: 2024/03/18 11:42:46 by dkaiser ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_printf.h"
+
+static void printhex_rec(unsigned int nbr, char fmt, int *len)
+{
+ char c;
+ int success;
+
+ 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)++;
+}
+
+int ft_printhex(unsigned int nbr, char fmt)
+{
+ int len;
+
+ len = 0;
+ printhex_rec(nbr, fmt, &len);
+ return (len);
+}