-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa_base.c
More file actions
82 lines (73 loc) · 1.95 KB
/
ft_itoa_base.c
File metadata and controls
82 lines (73 loc) · 1.95 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: malexand <malexand@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/03/20 23:15:57 by malexand #+# #+# */
/* Updated: 2017/02/16 17:34:19 by malexand ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_getlen(int value, int base)
{
int len;
len = 0;
while (value != 0)
{
value = value / base;
len++;
}
return (len);
}
static int ft_getstr(int value, int base, char *str)
{
int rest;
int count;
count = 0;
rest = 0;
while (value != 0)
{
rest = value % base;
str[count++] = (rest > 10) ? (rest - 10) + 'A' : rest + '0';
value = value / base;
}
return (count);
}
static int is_negative(int *value)
{
int sign;
sign = 1;
if (*value < 0)
{
*value = -*value;
sign = -1;
}
return (sign);
}
char *ft_itoa_base(int value, int base)
{
int sign;
int count;
char *str;
count = 0;
sign = is_negative(&value);
if (base < 2 || base > 16)
return ("");
if (value == 0 || value == -0)
return ("0");
if (value == -2147483648)
return ("-2147483648");
if (value == 2147483647)
return ("2147483647");
if ((str = ft_strnew(ft_getlen(value, base) + sign))
== NULL)
return (NULL);
count = ft_getstr(value, base, str);
if (sign < 0 && base == 10)
str[count++] = '-';
str[count] = '\0';
ft_strrev(str);
return (str);
}