Skip to content

Commit

Permalink
add binary support to ultoa
Browse files Browse the repository at this point in the history
  • Loading branch information
bemxio committed Sep 10, 2024
1 parent a54742b commit ea4b891
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions src/kernel/lib/stdlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,8 @@ char* __ultoa_dec(unsigned long n, char* buf) {
}

buf[i] = '\0';
strrev(buf);

return buf;
return strrev(buf);
}

char* __ultoa_hex(unsigned long n, char* buf) {
Expand All @@ -106,11 +105,30 @@ char* __ultoa_hex(unsigned long n, char* buf) {
return strrev(buf);
}

char* __ultoa_bin(unsigned long n, char* buf) {
unsigned char i = 0;

if (n == 0) {
buf[i++] = '0';
} else {
for (unsigned long x = n; x > 0; x /= 2) {
buf[i++] = x % 2 + 48;
}
}

buf[i] = '\0';

return strrev(buf);
}

char* ultoa(unsigned long n, char* buf, char base) {
if (base == 10) {
return __ultoa_dec(n, buf);
} else if (base == 16) {
return __ultoa_hex(n, buf);
switch (base) {
case 2:
return __ultoa_bin(n, buf);
case 10:
return __ultoa_dec(n, buf);
case 16:
return __ultoa_hex(n, buf);
}

return NULL;
Expand Down

0 comments on commit ea4b891

Please sign in to comment.