-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_utils.c
More file actions
100 lines (89 loc) · 2.05 KB
/
ft_utils.c
File metadata and controls
100 lines (89 loc) · 2.05 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tdehne <tdehne@student.42heilbronn.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/19 09:29:24 by tdehne #+# #+# */
/* Updated: 2022/04/28 16:53:45 by tdehne ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int log_10(unsigned int num)
{
unsigned int counter;
counter = 0;
num /= 10;
while (num != 0)
{
num /= 10;
counter++;
}
return (counter);
}
static int pow_of(int base, int exp)
{
int result;
result = 1;
while (exp > 0)
{
result *= base;
exp--;
}
return (result);
}
int putuint_fd(unsigned int n, int fd)
{
int digit;
int exp;
int counter;
exp = log_10(n);
counter = 0;
while (exp >= 0)
{
digit = ((n / pow_of(10, exp)) % 10) + 48;
write(fd, &digit, 1);
counter++;
exp--;
}
return (counter);
}
int putnbr_count(int n)
{
int digit;
int exp;
int counter;
long num_long;
int minus;
num_long = (long) n;
counter = 0;
minus = 0;
if (num_long < 0)
{
num_long = -num_long;
write(1, "-", 1);
minus = 1;
}
exp = log_10(num_long);
while (counter <= exp)
{
digit = ((num_long / pow_of(10, exp - counter)) % 10) + 48;
write(1, &digit, 1);
counter += 1;
}
return (counter + minus);
}
void write_hex(unsigned long long num, char *base, int *counter)
{
unsigned int rem;
if (num < 1)
{
return ;
}
rem = num % 16;
num = num / 16;
(*counter)++;
write_hex(num, base, counter);
write(1, &base[rem], 1);
}