-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconversion.c
More file actions
110 lines (104 loc) · 1.93 KB
/
Copy pathconversion.c
File metadata and controls
110 lines (104 loc) · 1.93 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
#include "main.h"
char handle_cap(int capital, int num);
/**
* print_int - function that convert integers
* @base: Fetches the argument base of the to coverted to
* @_case: Fetches if it uppercase or lowercase
* @num: fetches the integer to converted
*
* Return: The int has covert string
*/
char *print_int(unsigned int num, unsigned int base, int _case)
{
char *string;
unsigned int mul = 1, remainder = 0, lenght = 1, i = 0, is_negative = 0;
char character;
if (_case == 2)
{
is_negative = 1;
lenght++;
}
while ((num / mul) >= base)
{
mul *= base;
lenght++;
}
string = malloc(sizeof(char) * (lenght + 1));
if (string == NULL)
{
return (0);
}
if (is_negative)
{
string[i] = '-';
i++;
}
if (num >= base)
{
while ((num / mul) >= base)
{mul *= base;
lenght++;
}
string = malloc(sizeof(char) * (lenght + 1));
if (string == NULL)
{
return (NULL);
}
if (is_negative)
{
string[i] = '-';
i++;
}
if ((num / mul) >= 10)
character = handle_cap(_case, (num / mul));
else
character = (num / mul) + '0';
string[i] = character;
i++;
while (mul >= base)
{
mul /= base;
remainder = (num / mul) % base;
if (remainder >= 10)
{
character = handle_cap(_case, remainder);
}
else
character = remainder + '0';
string[i] = character;
i++;
}
}
else
{
string = malloc(sizeof(char) * (lenght + 1));
if (string == NULL)
{
return (NULL);
}
if (num >= 10)
string[i] = handle_cap(_case, num);
else
string[i] = num + '0';
}
if (string == NULL)
return (string = "(null)");
string[lenght] = '\0';
return (string);
}
/**
* handle_cap - handle capital conversion for hexadecimal
* @capital: if the result should be in capital
* @num: the number to convert
*
* Return: the character
*/
char handle_cap(int capital, int num)
{
char s;
if (capital == 0)
s = (num - 10) + 'a';
else if (capital == 1)
s = (num - 10) + 'A';
return (s);
}