Skip to content

Instantly share code, notes, and snippets.

@bw2012
Created January 21, 2025 21:11
Show Gist options
  • Save bw2012/0a78c743ee9dce3c7b8dd3f8af171672 to your computer and use it in GitHub Desktop.
Save bw2012/0a78c743ee9dce3c7b8dd3f8af171672 to your computer and use it in GitHub Desktop.
c++ base36 encoding example
#include <cstdio>
#include <string>
#include <cstdint>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstdlib>
char seq[] = "0123456789abcdefghijklmnopqrstuvwxyz";
std::string base36_encode(uint64_t in){
std::string result;
while (in != 0) {
result.push_back(seq[in % 36]);
in /= 36;
}
std::reverse(result.begin(), result.end());
return result;
}
int main(void) {
uint64_t in = ULLONG_MAX;
//uint64_t in = 9223372036854775807;
std::string r = base36_encode(in);
printf("%s\n", r.c_str());
char* end = nullptr;
uint64_t d = std::strtoull(r.c_str(), &end, 36);
printf("%llu\n", d);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment