setos

拙OS
Log | Files | Refs

commit ca933cd867515ac0a723c28a89bb0f2d53b1b033
parent e3e9af2641fa665696ab4c619da8bde622940749
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Sat, 16 Mar 2024 19:17:12 +0900

add hex to printf

Diffstat:
Mhello.c | 2+-
Mutils.c | 25+++++++++++++++++++++++--
2 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/hello.c b/hello.c @@ -15,7 +15,7 @@ EfiMain(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *systab) // Output firmware bender. SystemTable->ConOut->ClearScreen(SystemTable->ConOut); SystemTable->ConOut->OutputString(SystemTable->ConOut, SystemTable->FirmwareVendor); - printf("--%s--%d--\n==\n", "char *", 1259320); + printf("--%s--%d--\n==%x==\n", "char *", 1259320, 0xdeadbeef); // Output memory map information. UINTN mmsize = 8196; diff --git a/utils.c b/utils.c @@ -28,8 +28,9 @@ printf(char *fmt, ...) CHAR16 _buf[1024], *buf = _buf; EFI_STATUS stat; - char *s; - int d, e; + char *s; + long long d, e; + unsigned long long f, g, h; va_start(ap, fmt); for (;*fmt;) { @@ -62,12 +63,32 @@ printf(char *fmt, ...) d = -d; } for (e = 1; e <= d; e *= 10) { + if (e > e * 10) { // integer too big; + return -1; + } } for (e /= 10; e >= 1; e /= 10) { *buf++ = (CHAR16) ('0' + ((d / e) % 10)); } break; + case 'x': + f = va_arg(ap, unsigned int); + for (g = 1; g <= f; g *= 0x10) { + if (g > g * 0x10) { // integer too big. + return -1; + } + } + for (g /= 0x10; g >= 1; g /= 0x10) { + h = ((f / g) % 0x10); + if (f < 0xa) { + *buf++ = (CHAR16) ('0' + h); + } else { + *buf++ = (CHAR16) ('a' + h - 0xa); + } + } + break; } + } va_end(ap); *buf = L'\0';