Created
September 14, 2018 08:37
-
-
Save sleepdefic1t/9b9a712da907123ad6122490b2198558 to your computer and use it in GitHub Desktop.
unsigned long long with base to String
This file contains hidden or 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
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