implement %x in printf
This commit is contained in:
parent
ac64b124da
commit
50fa9b7d24
38
core/klibc.c
38
core/klibc.c
@ -17,3 +17,41 @@ void *memset(void *src, int c, size_t n)
|
||||
}
|
||||
return src;
|
||||
}
|
||||
|
||||
char * itoa( int value, char * str, int base )
|
||||
{
|
||||
char * rc;
|
||||
char * ptr;
|
||||
char * low;
|
||||
// Check for supported base.
|
||||
if ( base < 2 || base > 36 )
|
||||
{
|
||||
*str = '\0';
|
||||
return str;
|
||||
}
|
||||
rc = ptr = str;
|
||||
// Set '-' for negative decimals.
|
||||
if ( value < 0 && base == 10 )
|
||||
{
|
||||
*ptr++ = '-';
|
||||
}
|
||||
// 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;
|
||||
} while ( value );
|
||||
// Terminating the string.
|
||||
*ptr-- = '\0';
|
||||
// Invert the numbers.
|
||||
while ( low < ptr )
|
||||
{
|
||||
char tmp = *low;
|
||||
*low++ = *ptr;
|
||||
*ptr-- = tmp;
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
#include "stdarg.h"
|
||||
|
||||
void *memcpy(void *dest, const void *src, size_t n );
|
||||
void *memcpy(void *dest, const void *src, size_t n);
|
||||
void *memset(void *s, int c, size_t n);
|
||||
char *itoa(int value, char *str, int base);
|
||||
|
@ -104,6 +104,13 @@ void vprintf(const char *format, va_list ap)
|
||||
printInt(d);
|
||||
break;
|
||||
}
|
||||
case 'x': {
|
||||
char val[sizeof(int) * 2];
|
||||
int d = va_arg(ap, int);
|
||||
itoa(d, val, 16);
|
||||
printString(val);
|
||||
break;
|
||||
}
|
||||
case 'c': {
|
||||
int c = va_arg(ap, int);
|
||||
printChar((char)c);
|
||||
|
Loading…
Reference in New Issue
Block a user