A poorly written OS for the x86 arch. (WIP)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
eOS/lib/conv.c

49 lines
880 B

3 years ago
#include "conv.h"
3 years ago
#include "../drivers/vga.h"
3 years ago
3 years ago
char* itoa( int value, char * str, int base ) {
char* rc;
char* ptr;
char* low;
3 years ago
3 years ago
// Check for supported base.
3 years ago
if( base < 2 || base > 36 ) {
3 years ago
*str = '\0';
return str;
}
rc = ptr = str;
3 years ago
switch(base) {
case 16:
*ptr++ = '0';
*ptr++ = 'x';
case 10:
if(value < 0)
*ptr++ = '-';
}
// if(value < 0 && base == 10 ) // sign
// *ptr++ = '-';
3 years ago
// Remember where the numbers start.
low = ptr;
// The actual conversion.
do {
// Modulo is negative for negative value. This trick makes abs() unnecessary.
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + value % base];
value /= base;
3 years ago
} while(value);
3 years ago
// Terminating the string.
*ptr-- = '\0';
3 years ago
3 years ago
// Invert the numbers.
while ( low < ptr ) {
char tmp = *low;
*low++ = *ptr;
*ptr-- = tmp;
}
return rc;
3 years ago
}