1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
# include <iostream>
# include <cmath>
int main (int, char **) {
/* symbol table for place values. */
static char constexpr sym[36] = {
'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F',
'G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V',
'W','X','Y','Z'
};
/* Convert this value */
unsigned const N = 12941;
/* Output radix. rx <= sizeof(sym) */
static unsigned constexpr rx = 2;
std::cout << N << " = ";
for (int place = std::floor(std::log(N) / std::log(rx)); place >= 0; --place)
std::cout << sym[static_cast<unsigned int>(N / std::pow(rx, place)) % rx];
std::cout << "\n";
}
|