-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstdlib.cpp
More file actions
49 lines (40 loc) · 900 Bytes
/
stdlib.cpp
File metadata and controls
49 lines (40 loc) · 900 Bytes
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
/**
* @file stdlib.cpp
* @brief Standard Library Implementation
*
* @date 01/02/2026
* @version 1.0.0
*/
#include <stdlib.h>
char* itoa(uint32_t num, char* str, uint32_t base) {
uint32_t i = 0;
bool isNegative = false;
if (num == 0) {
str[i++] = '0';
str[i] = '\0';
return str;
}
if (num < 0 && base == 10) {
isNegative = true;
num = -num;
}
while (num != 0) {
uint32_t rem = num % base;
str[i++] = (rem > 9) ? (rem - 10) + 'A' : rem + '0';
num = num / base;
}
if (isNegative) {
str[i++] = '-';
}
str[i] = '\0';
// Reverse the string
uint32_t start = 0, end = i - 1;
while (start < end) {
char temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
return str;
}