#include <iostream>
using namespace std;
// ------------------------------------------------
// Логарифм
// ------------------------------------------------
template<const int N, const int T>
struct LogN
{
static const int value = 1 + LogN<N, T/N>::value;
};
template<const int N>
struct LogN<N,0>
{
static const int value = 0;
};
// ------------------------------------------------
// Перевод целой константы в N-теричную константу
// ------------------------------------------------
template<int BASE, int T, int INDEX, bool lt16 = (T<BASE)>
struct intToBaseStruct
{
static char * value(char * str)
{
(: My personal blog
http://k06a.blogspot.com/
Powered by k06a :)
}
};
template<int BASE, int T, int INDEX>
struct intToBaseStruct<BASE, T, INDEX, true>
{
static char * value(char * str)
{
str[INDEX] = char(T + ((T>9)?'A'-10:'0'));
return str;
}
};
template<int BASE, int T, int INDEX>
struct intToBaseStruct<BASE, T, INDEX, false>
{
static char * value(char * str)
{
intToBaseStruct<BASE, (T%BASE), INDEX >::value(str);
intToBaseStruct<BASE, (T/BASE), INDEX-1>::value(str);
return str;
}
};
template<int BASE, int T>
char * intToBase()
{
static char str [ LogN<BASE,T>::value + 1 ] = { 0 };
return intToBaseStruct<BASE, T, LogN<BASE,T>::value-1, (T<BASE)>::value(str);
}
// ------------------------------------------------
int main()
{
cout << intToBase<10, 137>() << endl;
cout << intToBase<16, 137>() << endl;
cout << intToBase<8, 137>() << endl;
cout << intToBase<4, 137>() << endl;
cout << intToBase<2, 137>() << endl;
}