Added string.h header with itoa function that converts an int to a ASCII string

This commit is contained in:
2024-03-11 20:27:15 +01:00
parent d907df625f
commit 18f33ce7db
2 changed files with 26 additions and 0 deletions
+18
View File
@@ -0,0 +1,18 @@
#include "string.h"
char* itoa(uint32_t val, uint8_t base) {
static char buf[32] = { 0 };
if (base == 0 || base > 16) {
// TODO: Add errors!!
return buf;
}
int i = 30;
for (; val && i; i--, val /= base) {
buf[i] = "0123456789abcdef"[val % base];
}
return &buf[i + 1];
}
+8
View File
@@ -0,0 +1,8 @@
#ifndef STRING_H
#define STRING_H
#include "stdint.h"
char* itoa(uint32_t val, uint8_t base);
#endif