-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_digit_to_string.c
102 lines (94 loc) · 2.86 KB
/
ft_digit_to_string.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
98
99
100
101
102
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_digit_to_string.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: minkim3 <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/03 20:39:13 by minkim3 #+# #+# */
/* Updated: 2023/01/25 20:49:26 by minkim3 ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_decimal_to_string(t_options *string_info, int decimal)
{
if (decimal == INT_MIN)
{
string_info->val_minus = 1;
string_info->value = ft_strdup("2147483648");
if (string_info->value == NULL)
return (ft_malloc_error_void(string_info));
return ;
}
if (decimal < 0)
{
string_info->val_minus = 1;
decimal *= -1;
}
string_info->value = ft_itoa(decimal);
if (string_info->value == NULL)
return (ft_malloc_error_void(string_info));
}
static int count_digit_number_for_hexadecimal(unsigned long long decimal)
{
int digit_number;
digit_number = 0;
while (decimal > 0)
{
decimal /= 16;
digit_number++;
}
return (digit_number);
}
void ft_decimal_to_hexadecimal(t_options *string_info, unsigned int decimal)
{
int digit_number;
char *result;
if (decimal == 0)
{
string_info->value = ft_strdup("0");
if (string_info->value == NULL)
return (ft_malloc_error_void(string_info));
return ;
}
digit_number = \
count_digit_number_for_hexadecimal((unsigned long long)decimal);
result = ft_new_string(digit_number + 1);
if (result == NULL)
return (ft_malloc_error_void(string_info));
result[digit_number--] = '\0';
while (decimal > 0)
{
if (string_info->type == 'x')
result[digit_number--] = "0123456789abcdef"[decimal % 16];
else if (string_info->type == 'X')
result[digit_number--] = "0123456789ABCDEF"[decimal % 16];
decimal /= 16;
}
string_info->value = result;
}
char *ft_pointer_to_lowercase_hexadecimal(unsigned long long decimal)
{
char *result;
size_t digit_number;
if (decimal == 0)
{
result = ft_strdup("0");
return (result);
}
digit_number = \
count_digit_number_for_hexadecimal((unsigned long long)decimal) + 2;
result = ft_new_string(digit_number + 1);
if (result == NULL)
return (NULL);
result[digit_number] = '\0';
digit_number--;
while (decimal > 0)
{
result[digit_number--] = "0123456789abcdef"[decimal % 16];
decimal /= 16;
}
result[digit_number--] = 'x';
result[digit_number] = '0';
return (result);
}