diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-11-05 15:33:08 -0500 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-11-05 15:33:08 -0500 |
| commit | a8a35ea688b07a6cd3e342ad75e045b7433d294b (patch) | |
| tree | 385a6f7de6fedd54b0e0678d778e6908f67aed19 /stdlib/bytes.c | |
| parent | b238f1df41eb4f3badd69abc9ae0b5dc857e58a6 (diff) | |
Add Byte.hex()
Diffstat (limited to 'stdlib/bytes.c')
| -rw-r--r-- | stdlib/bytes.c | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/stdlib/bytes.c b/stdlib/bytes.c index f36a56ba..694173b9 100644 --- a/stdlib/bytes.c +++ b/stdlib/bytes.c @@ -17,6 +17,19 @@ PUREFUNC public Text_t Byte$as_text(const Byte_t *b, bool colorize, const TypeIn return Text$format(colorize ? "\x1b[36mByte\x1b[m(\x1b[35m0x%02X\x1b[m)" : "Byte(0x%02X)", *b); } +public Text_t Byte$hex(Byte_t byte, bool uppercase, bool prefix) { + Text_t text = {.tag=TEXT_SHORT_ASCII}; + if (prefix && uppercase) + text.length = (int64_t)snprintf(text.short_ascii, sizeof(text.short_ascii), "0x%02X", byte); + else if (prefix && !uppercase) + text.length = (int64_t)snprintf(text.short_ascii, sizeof(text.short_ascii), "0x%02x", byte); + else if (!prefix && uppercase) + text.length = (int64_t)snprintf(text.short_ascii, sizeof(text.short_ascii), "%02X", byte); + else if (!prefix && !uppercase) + text.length = (int64_t)snprintf(text.short_ascii, sizeof(text.short_ascii), "%02x", byte); + return text; +} + public const TypeInfo_t Byte$info = { .size=sizeof(Byte_t), .align=__alignof__(Byte_t), |
