aboutsummaryrefslogtreecommitdiff
path: root/convert_hex.c
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2019-10-30 18:07:21 +0100
committerCharles <sircharlesaze@gmail.com>2019-10-30 18:07:21 +0100
commit22f334a19cabebf10727d7894102946ba23d0e37 (patch)
treefd323ff0dbb8fc06a00c8c26dfc6fbd3b5ae0910 /convert_hex.c
parent001786c8ec464b1ae3e6321acfd984227cb1bbee (diff)
downloadft_printf-22f334a19cabebf10727d7894102946ba23d0e37.tar.gz
ft_printf-22f334a19cabebf10727d7894102946ba23d0e37.tar.bz2
ft_printf-22f334a19cabebf10727d7894102946ba23d0e37.zip
Fixed %d segfault, merge hex_* in hex, fixed c = 0
Diffstat (limited to 'convert_hex.c')
-rw-r--r--convert_hex.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/convert_hex.c b/convert_hex.c
new file mode 100644
index 0000000..457b249
--- /dev/null
+++ b/convert_hex.c
@@ -0,0 +1,40 @@
+#include <stdarg.h>
+#include <stdlib.h>
+#include "header.h"
+
+char *convert_hex(va_list ap, t_pformat *pformat)
+{
+ long long unsigned int n;
+
+ if (pformat->flags & FLAG_SHORT)
+ n = va_arg(ap, int);
+ else if (pformat->flags & FLAG_SHORT_SHORT)
+ n = va_arg(ap, int);
+ else if (pformat->flags & FLAG_LONG)
+ n = va_arg(ap, long unsigned int);
+ else if (pformat->flags & FLAG_LONG_LONG)
+ n = va_arg(ap, long long unsigned int);
+ else
+ n = va_arg(ap, unsigned int);
+
+ char *str;
+ str = ITOA_HEX_LOW(n);
+ if (pformat->type == 'X')
+ ft_strtoupper(str);
+ str = handle_precision(pformat, str);
+ if (pformat->flags & FLAG_ZERO_PADDING)
+ {
+ if (pformat->flags & FLAG_ALTERNATE && n != 0)
+ pformat->min_width -= 2;
+ str = handle_padding(pformat, str);
+ }
+ if (pformat->flags & FLAG_ALTERNATE && n != 0)
+ {
+ char *tmp = ft_strjoin(pformat->type == 'X' ? "0X" : "0x", str);
+ free(str);
+ str = tmp;
+ }
+ if (!(pformat->flags & FLAG_ZERO_PADDING))
+ str = handle_padding(pformat, str);
+ return (str);
+}