Skip to content

Instantly share code, notes, and snippets.

@alivesay
Created May 20, 2021 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alivesay/673451a23b5998017f6d66fbf628f03c to your computer and use it in GitHub Desktop.
Save alivesay/673451a23b5998017f6d66fbf628f03c to your computer and use it in GitHub Desktop.
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