Improve dec_to_binary function

This commit is contained in:
Enrico Fraccaroli (Galfurian)
2023-07-12 14:26:14 -04:00
parent dc5a30573c
commit 707c0e408b
2 changed files with 15 additions and 5 deletions
+8 -3
View File
@@ -18,7 +18,7 @@ static int max_log_level = LOGLEVEL_DEBUG;
void dbg_putchar(char c)
{
outportb(SERIAL_COM1, (uint8_t)c);
outportb(SERIAL_COM1, (unsigned char)c);
}
void dbg_puts(const char *s)
@@ -142,9 +142,14 @@ const char *to_human_size(unsigned long bytes)
const char *dec_to_binary(unsigned long value, unsigned length)
{
static char buffer[33];
memset(buffer, 0, 33);
for (int i = 0, j = 32 - min(max(0, length), 32); j < 32; ++i, ++j)
// Adjust the length.
length = min(max(0, length), 32U);
// Build the binary.
for (unsigned i = 0, j = 32U - length; j < 32U; ++i, ++j) {
buffer[i] = bit_check(value, 31 - j) ? '1' : '0';
}
// Close the string.
buffer[length] = 0;
return buffer;
}
+7 -2
View File
@@ -143,9 +143,14 @@ const char *to_human_size(unsigned long bytes)
const char *dec_to_binary(unsigned long value, unsigned length)
{
static char buffer[33];
memset(buffer, 0, 33);
for (int i = 0, j = 32 - min(max(0, length), 32); j < 32; ++i, ++j)
// Adjust the length.
length = min(max(0, length), 32U);
// Build the binary.
for (unsigned i = 0, j = 32U - length; j < 32U; ++i, ++j) {
buffer[i] = bit_check(value, 31 - j) ? '1' : '0';
}
// Close the string.
buffer[length] = 0;
return buffer;
}