Skip to content

Instantly share code, notes, and snippets.

@sleepdefic1t
Created September 14, 2018 08:37
Show Gist options
  • Save sleepdefic1t/9b9a712da907123ad6122490b2198558 to your computer and use it in GitHub Desktop.
Save sleepdefic1t/9b9a712da907123ad6122490b2198558 to your computer and use it in GitHub Desktop.
unsigned long long with base to String
std::string toString(unsigned long long n, uint8_t base)
{
unsigned char buf[16 * sizeof(long)]; // Assumes 8-bit chars.
unsigned long long i = 0;
if (n == 0) { return "0"; };
while (n > 0) {
buf[i++] = n % base;
n /= base;
}
std::string stringBuffer;
for (; i > 0; i--) {
stringBuffer += (
buf[i - 1] < 10
? '0' + buf[i - 1]
: 'A' + buf[i - 1] - 10
);
};
return stringBuffer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment