Created
May 20, 2021 16:03
-
-
Save alivesay/673451a23b5998017f6d66fbf628f03c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inline int abs(int n) { return (n < 0) ? -n : n; } | |
char* itoa(int Value, int Base) { | |
static const char lookup[] = "0123456789ABCDEF"; | |
static const size_t bufferSize = 65; | |
static char buf[bufferSize] = {0}; | |
int i = bufferSize - 1; | |
int n = Value; | |
do { | |
buf[--i] = lookup[abs(n % Base)]; | |
} while (n /= Base); | |
if (Base == 10 && Value < 0) buf[--i] = '-'; | |
int len = bufferSize - i; | |
char* ascii = new char[len]; | |
while (--len >= 0) { | |
ascii[len] = buf[i + len]; | |
} | |
return ascii; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment