So here are some very light recursive functions that will convert almost any generic datatype into it's appropriate base.
void toBinary(byte t)
{
if(t)
{
toBinary(t>>1);
cout << (t & 1);
}
}
{
if(t)
{
toBinary(t>>1);
cout << (t & 1);
}
}
Binary is the simplest conversion since you simply compare the 1s and 0s.
void toOctal(byte t)
{
cout << '0';
toOctalR(t);
}
void toOctalR(byte t)
{
if(t)
{
toOctalR(n>>3);
cout << (n & 7);
}
}
void toHex(int n)
{
cout << "0x";
toHexR(n);
}
{
cout << '0';
toOctalR(t);
}
void toOctalR(byte t)
{
if(t)
{
toOctalR(n>>3);
cout << (n & 7);
}
}
void toHex(int n)
{
cout << "0x";
toHexR(n);
}
Hex conversions are probably the hardest since you would need to call forth different alpha characters to represent 10, 11, 12, 13, 14, and 15. You could do a switch but this seems a bit more elegant imo.
void toHexR(int n)
{
if(n)
{
toHexR(n>>4);
if((n & 15) < 10)
cout << (n & 15);
else
cout << static_cast<char>((n & 15) + 55);
}
}
{
if(n)
{
toHexR(n>>4);
if((n & 15) < 10)
cout << (n & 15);
else
cout << static_cast<char>((n & 15) + 55);
}
}
The +55 is the ASCII offset.
I'm not sure if this will ever be useful to anyone here but in case you want to write an encryption or decryption script these are pretty light.</char>