print integers and doubles

still need to add hex but i gtg so ill do that later
This commit is contained in:
2026-01-28 10:33:13 +11:00
parent 32453f3c0b
commit 8d9002d3ca
8 changed files with 102 additions and 1 deletions

90
kernel/src/cstr.cpp Normal file
View File

@@ -0,0 +1,90 @@
#include "cstr.h"
char uintTo_StringOutput[128];
const char* to_string(uint64_t value) {
uint8_t size = 0;
uint64_t sizeTest = value;
while (sizeTest / 10 > 0) {
sizeTest /= 10;
size++;
}
uint8_t index = 0;
while (value / 10 > 0) {
uint8_t remainder = value % 10;
value /= 10;
uintTo_StringOutput[size - index] = remainder + '0';
index++;
}
uint8_t remainder = value % 10;
uintTo_StringOutput[size - index] = remainder + '0';
uintTo_StringOutput[size + 1] = 0;
return uintTo_StringOutput;
}
char intTo_StringOutput[128];
const char* to_string(int64_t value) {
uint8_t isNegative = 0;
if (value < 0) {
isNegative = 1;
value *= -1;
intTo_StringOutput[0] = '-';
}
uint8_t size = 0;
uint64_t sizeTest = value;
while (sizeTest / 10 > 0) {
sizeTest /= 10;
size++;
}
uint8_t index = 0;
while (value / 10 > 0) {
uint8_t remainder = value % 10;
value /= 10;
intTo_StringOutput[isNegative + size - index] = remainder + '0';
index++;
}
uint8_t remainder = value % 10;
intTo_StringOutput[isNegative + size - index] = remainder + '0';
intTo_StringOutput[isNegative + size + 1] = 0;
return intTo_StringOutput;
}
char doubleTo_StringOutput[128];
const char* to_string(double value, uint8_t decimalPlaces) {
char* intPtr = (char*)to_string((int64_t)value);
char* doublePtr = doubleTo_StringOutput;
if (value < 0) {
value *= -1;
}
while (*intPtr != 0) {
*doublePtr = *intPtr;
intPtr++;
doublePtr++;
}
*doublePtr = '.';
doublePtr++;
double newValue = value - (int)value; // get only the decimal places
for (uint8_t i = 0; i < decimalPlaces; i++) {
newValue *= 10;
*doublePtr = (int)newValue + '0';
newValue -= (int)newValue;
doublePtr++;
}
*doublePtr = 0;
return doubleTo_StringOutput;
}

6
kernel/src/cstr.h Normal file
View File

@@ -0,0 +1,6 @@
#pragma once
#include <stdint.h>
const char* to_string(uint64_t value);
const char* to_string(int64_t value);
const char* to_string(double value, uint8_t decimalPlaces);

View File

@@ -1,6 +1,7 @@
#include <stddef.h>
#include <stdint.h>
#include "BasicRenderer.h"
#include "cstr.h"
// supposed to protect against stack smashing but we have no way to actually like
@@ -11,7 +12,11 @@ extern "C" void __stack_chk_fail(void) {
extern "C" void _start(Framebuffer* framebuffer, PSF1_FONT* psf1_Font) {
BasicRenderer newRenderer = BasicRenderer(framebuffer, psf1_Font);
newRenderer.Print(0xFFFFFF, "Hello, World!");
newRenderer.Print(0xFFFFFF, to_string((uint64_t)1234976));
newRenderer.cursorPosition = {0, 18};
newRenderer.Print(0xFFFFFF, to_string((int64_t)-1234976));
newRenderer.cursorPosition = {0, 36};
newRenderer.Print(0xFFFFFF, to_string(3.141, 2));
return;
}