-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
97 lines (86 loc) · 1.78 KB
/
utils.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
89
90
91
92
93
94
95
96
97
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ogenc <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/26 00:09:51 by ogenc #+# #+# */
/* Updated: 2023/03/07 20:53:53 by ogenc ### ########.fr */
/* */
/* ************************************************************************** */
#include"minitalk.h"
int ft_atoi(char *str)
{
int i;
int num;
i = 0;
num = 0;
while (str[i] != '\0')
{
num *= 10;
num += str[i] - 48;
i++;
}
return (num);
}
int ft_strlen(char *str)
{
int i;
i = 0;
while (str[i])
i++;
return (i);
}
void ft_putnbr(int nb)
{
char result;
if (nb >= 0 && nb <= 9)
{
result = nb + 48;
write (1, &result, 1);
}
else if (nb >= 10)
{
ft_putnbr(nb / 10);
result = (nb % 10) + 48;
write (1, &result, 1);
}
}
char *itoa_pid(int nb)
{
int i;
int x;
char *str;
i = 0;
x = nb;
while (x)
{
x /= 10;
i++;
}
str = malloc(i + 2);
str[i + 1] = '\0';
str[i] = 27;
while (--i >= 0)
{
str[i] = (nb % 10) + '0';
nb = nb / 10;
}
return (str);
}
char *convertbinary(unsigned char a)
{
int i;
char *binary;
i = 7;
binary = malloc(8 + 1);
while (i >= 0)
{
binary[i] = (a % 2) + '0';
a = a / 2;
i--;
}
binary[8] = 0;
return (binary);
}