-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printers3.c
88 lines (79 loc) · 1.91 KB
/
ft_printers3.c
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
87
88
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printers3.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lrosch <lrosch@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/09/06 14:37:14 by lrosch #+# #+# */
/* Updated: 2022/01/13 15:43:22 by lrosch ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
/* easy number writing function for integers */
void ft_putnbr(int n)
{
if (n == -2147483648)
write(1, "-2147483648", 11);
else
{
if (n < 0)
{
ft_putchar('-');
n = n * -1;
}
if (n < 10)
ft_putchar('0' + n);
else
{
ft_putnbr(n / 10);
ft_putnbr(n % 10);
}
}
}
/* helper function for unsigned int return values */
int ft_get_u_nbrlen(unsigned int i)
{
int j;
j = 0;
if (i == 0)
return (1);
while (i != 0)
{
i = i / 10;
j++;
}
return (j);
}
/*
** decimal to hexadecimal conversion for pointer
** difference between ptrdectohex and normal dectohex
** is the variable type in unsigned long and unsigned int
*/
char *ft_ptrdectohex(unsigned long dec)
{
unsigned long rest;
char *hex;
int i;
i = 0;
hex = malloc(ft_hexlen(dec) + 1);
if (hex == NULL)
return (NULL);
while (dec != 0)
{
rest = dec % 16;
if (rest < 10)
{
hex[i] = 48 + rest;
i++;
}
else
{
hex[i] = 55 + rest;
i++;
}
dec = dec / 16;
}
hex[i] = '\0';
return (ft_reverse(hex));
}