-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_convert_unsigned_long_int.c
executable file
·80 lines (75 loc) · 2.21 KB
/
ft_convert_unsigned_long_int.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_convert_unsigned_long_int.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ngouy <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/01/13 13:07:08 by ngouy #+# #+# */
/* Updated: 2015/02/13 09:46:54 by ngouy ### ########.fr */
/* */
/* ************************************************************************** */
#include "header.h"
static void to_smtint(unsigned long int n, int *sign, char **ret, int i)
{
if (n < 0)
{
(*ret)[*sign] = '-';
n = -n;
(*sign)++;
}
if (n >= (i % 100))
{
to_smtint(n / (i % 100), sign, ret, i);
(*sign)++;
if (n % (i % 100) > 9)
(*ret)[*sign] = (i > 100) ? (n % (i % 100) + 55) :
(n % i + 55 + (97 - 65));
else
(*ret)[*sign] = n % (i % 100) + 48;
}
else
(*ret)[*sign] = n + 48;
}
static void to_smtuns(long unsigned n, int *sign, char **ret, int i)
{
if (n >= (unsigned)(i % 100))
{
to_smtuns(n / (i % 100), sign, ret, i);
(*sign)++;
if (n % (i % 100) > 9)
(*ret)[*sign] = (i > 100) ? (n % (i % 100) + 55) :
(n % i + 55 + (97 - 65));
else
(*ret)[*sign] = n % (i % 100) + 48;
}
else
{
if (n % (i % 100) > 9)
(*ret)[*sign] = (i > 100) ? (n + 55) : (n + 55 + (97 - 65));
else
(*ret)[*sign] = n % (i % 100) + 48;
}
}
char *unsigned_long_int_to_smt(unsigned long int i, char c)
{
int sign;
char *str;
int j;
sign = 0;
j = -1;
str = malloc(sizeof(char) * 30);
while (sign != 30)
str[sign++] = '\0';
sign = 0;
(c == 'D') ? (j = 10) : j;
(c == 'O') ? (j = 8) : j;
(c == 'U') ? (j = 10) : j;
(c == 'x') ? (j = 16) : j;
(c == 'X') ? (j = 116) : j;
if (c == 'D')
to_smtint(i, &sign, &str, j);
else
to_smtuns((unsigned long)i, &sign, &str, j);
return (str);
}