-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_utils.c
More file actions
116 lines (101 loc) · 2.76 KB
/
Copy pathft_utils.c
File metadata and controls
116 lines (101 loc) · 2.76 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tbui-quo <tbui-quo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/25 14:12:14 by tbui-quo #+# #+# */
/* Updated: 2023/01/30 17:37:42 by tbui-quo ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_putchar(char c)
{
write (1, &c, 1);
return (1);
}
int ft_putstr(char *s)
{
int strlen;
if (s == 0)
{
return (ft_putstr("(null)"));
}
strlen = 0;
while (*s != 0)
{
ft_putchar(*s);
s++;
strlen++;
}
return (strlen);
}
int ft_putnbr_base(long int n, const char *base, int nbr_base, int len)
{
if (n < 0)
{
write(1, "-", 1);
n = -n;
len++;
}
if (n >= nbr_base)
{
len = ft_putnbr_base(n / nbr_base, base, nbr_base, len + 1);
}
else
len++;
write(1, &base[n % nbr_base], 1);
return (len);
}
int ft_putnbr_uns(long unsigned int n, const char *base, int nbr_base, int len)
{
if (n >= (long unsigned int) nbr_base)
{
len = ft_putnbr_base(n / nbr_base, base, nbr_base, len + 1);
}
else
len++;
write(1, &base[n % nbr_base], 1);
return (len);
}
int ft_print_pointer(void *ptr)
{
int len;
int prefix_offset;
len = 0;
prefix_offset = 2;
ft_putstr("0x");
return (ft_putnbr_uns((long unsigned int) ptr, "0123456789abcdef", 16,
len + prefix_offset));
}
// int main(void)
// {
// long int decimal_num = -255;
// long int hex_num = 1234567890;
// const char *decimal_base = "0123456789";
// const char *hex_base = "0123456789abcdef";
// printf("Decimal number: %ld\n", decimal_num);
// printf("Decimal base representation: ");
// int len_dec = ft_putnbr_base(decimal_num, decimal_base, 10, 0);
// printf("len dec = %d\n", len_dec);
// printf("Hexadecimal number: %ld\n", hex_num);
// printf("Hexadecimal base representation: ");
// int len_hex = ft_putnbr_base(hex_num, hex_base, 16, 0);
// printf("len hex = %d\n", len_hex);
// return 0;
// }
// int main(void)
// {
// ft_putstr(NULL);
// // printf("%s", NULL);
// }
// int main(void)
// {
// int x = 42;
// void *ptr = &x;
// ft_print_pointer(ptr);
// // printf("\n");
// // printf("%d", printf("%p", ptr));
// return 0;
// }