From 18f33ce7db1caf5222e9f848c8d6a99998bc729c Mon Sep 17 00:00:00 2001 From: Tom-on64 Date: Mon, 11 Mar 2024 20:27:15 +0100 Subject: [PATCH] Added string.h header with itoa function that converts an int to a ASCII string --- src/kernel/string.c | 18 ++++++++++++++++++ src/kernel/string.h | 8 ++++++++ 2 files changed, 26 insertions(+) create mode 100644 src/kernel/string.c create mode 100644 src/kernel/string.h diff --git a/src/kernel/string.c b/src/kernel/string.c new file mode 100644 index 0000000..740dc2b --- /dev/null +++ b/src/kernel/string.c @@ -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]; +} + diff --git a/src/kernel/string.h b/src/kernel/string.h new file mode 100644 index 0000000..8e02a23 --- /dev/null +++ b/src/kernel/string.h @@ -0,0 +1,8 @@ +#ifndef STRING_H +#define STRING_H + +#include "stdint.h" + +char* itoa(uint32_t val, uint8_t base); + +#endif