Implementing unsigned itoa (uint to str)

This commit is contained in:
Jarkko Toivanen 2023-06-18 06:25:19 +03:00
parent 1a9201b529
commit 5bfb8d9e97
Signed by: jt
GPG Key ID: 9151B109B73ECAD5
2 changed files with 25 additions and 0 deletions

View File

@ -24,6 +24,30 @@ char* itoa(int value, int base) {
}
return result;
}
char* uitoa(unsigned int value, int base) {
char* result;
// check that the base if valid
if (base < 2 || base > 36) { *result = '\0'; return result; }
char* ptr = result, *ptr1 = result, tmp_char;
int tmp_value;
do {
tmp_value = value;
value /= base;
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
} while ( value );
*ptr-- = '\0';
while(ptr1 < ptr) {
tmp_char = *ptr;
*ptr--= *ptr1;
*ptr1++ = tmp_char;
}
return result;
}
char* ltoa(long value, int base) {
char* result;

View File

@ -2,6 +2,7 @@
#define HEADER_XTOA
char* itoa(int value, int base);
char* uitoa(unsigned int value, int base);
char* ltoa(long value, int base);
char* ultoa(unsigned long value, int base);