Repository navigation
Expand file tree
/
Copy pathprint_x.c
More file actions
86 lines (79 loc) · 2.16 KB
/
Copy pathprint_x.c
File metadata and controls
86 lines (79 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_x.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lhernand <lhernand@student.42.us.or> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/01/29 00:42:19 by lhernand #+# #+# */
/* Updated: 2018/01/31 20:31:06 by scamargo ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
#include <stdio.h>
static void initialize(int *i, int *rem, int *len, long *num, char *str)
{
*i = 0;
*rem = 0;
*num = 0;
*len = ft_strlen(str);
}
static char *print_hex(char *str, int *l, t_bag *bag, long test)
{
int i;
int rem;
int len;
long num;
char *number;
initialize(&i, &rem, &len, &num, str);
number = ft_strnew(len);
// num = ft_atoi(str);
num = test;
if (num == 0)
number[i] = '\0';
while (num > 0)
{
rem = num % 16;
if (rem > 9)
{
if (bag->format_conversion == 'X')
number[i++] = (rem - 10) + 65;
else
number[i++] = (rem - 10) + 97;
}
else
number[i++] = rem + 48;
num = num / 16;
}
ft_strrev(number);
return (number);
}
void print_x(t_bag *bag, va_list args, int *i)
{
int len;
char *str;
char *res;
char *new;
long test;
test = (unsigned long)va_arg(args, long);
// str = ft_itoa(va_arg(args, long));
printf("%ld\n", test);
str = ft_itoa(test);
str = ft_itoa(va_arg(args, long));
len = ft_strlen(str);
printf("sting right here -> %s\n", str);
res = print_hex(str, &len, bag, test);
if (bag->number_sign)
{
if (bag->format_conversion == 'X')
new = ft_strjoin("0X", res);
else
new = ft_strjoin("0x", res);
len += 2;
}
else
new = ft_strjoin("", res);
*i += len - 1;
ft_putstr(new);
free(new);
}