-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_converter.c
49 lines (44 loc) · 1.33 KB
/
binary_converter.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* binary_converter.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mkhaing <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/20 02:28:31 by mkhaing #+# #+# */
/* Updated: 2023/11/20 07:36:43 by mkhaing ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
char ft_binary_to_char(const char *binary)
{
char result;
int i;
result = 0;
i = 0;
while (i < 8 && binary[i] != '\0')
{
result = (result << 1) + (binary[i] - 48);
i++;
}
return (result);
}
char *ft_char_to_binary(char c)
{
int i;
char *binary;
binary = (char *)malloc(9);
if (!binary)
{
return (NULL);
}
binary[8] = '\0';
i = 7;
while (i >= 0)
{
binary[i] = (c & 1) + 48;
c >>= 1;
i--;
}
return (binary);
}