blob: b8aa73e38b378091fcddc2608ff109096ab0abd7 (
plain) (
blame)
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
|
#include <stdint.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
static const char ITOA_TABLE[] = "0123456789";
char *itoa(int n)
{
if (!n) {
char *ret = (char *)malloc(2);
ret[0] = '0';
ret[1] = 0;
return ret;
}
u32 negative = (u32)(n < 0);
if (negative)
n *= -1;
int sz;
for (sz = 0; n % pow(10, sz) != n; sz++) {
}
char *ret = (char *)malloc((u32)(sz + 1));
for (int i = 0; i < sz; i++) {
int digit = (n % pow(10, i + 1)) / pow(10, i);
ret[i] = ITOA_TABLE[digit];
}
ret[sz] = 0;
if (negative) {
char *aux = (char *)malloc((u32)(sz + 2));
strcpy(aux, ret);
aux[sz] = '-';
aux[sz + 1] = 0;
free(ret);
ret = aux;
}
strinv(ret);
return ret;
}
|