Untitled_Kernel/src/kttools.c

53 lines
1.3 KiB
C
Raw Normal View History

#include "kttools.h"
#include <stdint.h>
/*
* VARS
*/
//Placeholder.
/*
* FUNCTIONS
*/
//convert digits from int num to buffer buf. in hex. This is 32 bit only for now!
//WARN: integer is written from right to left into the buffer, and will halt if the buffer is too small.
/**
* @brief Convert a hex int to string, up to 32 bits.
*
* Converts int "num" into a null-terminated string, placed into buffer "buf".
* Evaluates from right to left, so left hand digits will be cut off if the buffer is too small
* Should work for non 32-bit integers actually, but is uint32_t for now.
*
* @param num: Value to convert to string
* @param buf: Memory to place the string into
* @param size: Size of the memory buffer
*
* @return No return value
*
*/
void i_to_str(uint32_t num, char* buf, int size, int radix) {
//null terminate the string
buf[--size] = '\0';
while(size > 0 && (num) != 0){
int isolated_num = num % radix;
if(isolated_num > 9){
isolated_num+=7;
}
buf[--size] = '0' + isolated_num;
num/=radix;
}
//now shift the whole thing to the left
if(size != 0) {
while(buf[size]!='\0') {
*buf = buf[size];
buf++;
}
*buf = '\0';
}
}