#include <stdio.h>
typedef enum
{
BASE_DECIMAL = 10,
BASE_HEXADECIMAL = 16,
BASE_BINARY = 2,
BASE_ENUM_COUNT = 3
} Bases;
int Pow(int x, int exponent) // TODO: should this be inline?
{
int base_num = x;
if(exponent)
{
for(int i = 1; i < exponent; i++)
{
x = x * base_num;
}
return x;
}
else
{
return 1;
}
}
void
IntToAscii(char *output_buffer, const int number, const Bases base) {
int num = number;
int temp_num = 0;
int digit = 0;
int total_digits = 0;
int divisor = 0;
if (num == 0)
{
*output_buffer = '0';
return;
} else if(num < 0)
{
*output_buffer = '-';
output_buffer++;
num = num * -1;
}
temp_num = num;
while (temp_num > 0) {
temp_num = temp_num / base;
total_digits++;
}
total_digits = total_digits - 1;
for (int x = total_digits; x >= 0; x--)
{
divisor = Pow(base, x);
digit = num / divisor;
if(digit < 10 || base == BASE_DECIMAL)
{
*output_buffer = (char)(digit + '0');
}
else if(base == BASE_HEXADECIMAL)
{
digit = digit - 10;
*output_buffer = (char)(digit + 'A');
}
num = num - (digit * divisor);
output_buffer++;
}
}
int main()
{
char text[256] = {};
IntToAscii(text, 0x5eadbeef, BASE_HEXADECIMAL);
printf("%s\n", text);
}
I2luY2x1ZGUgPHN0ZGlvLmg+Cgp0eXBlZGVmIGVudW0KewogICAgQkFTRV9ERUNJTUFMID0gMTAsCiAgICBCQVNFX0hFWEFERUNJTUFMID0gMTYsCiAgICBCQVNFX0JJTkFSWSA9IDIsCiAgICBCQVNFX0VOVU1fQ09VTlQgPSAzCn0gQmFzZXM7CgppbnQgUG93KGludCB4LCBpbnQgZXhwb25lbnQpICAgLy8gVE9ETzogc2hvdWxkIHRoaXMgYmUgaW5saW5lPwp7CiAgICBpbnQgYmFzZV9udW0gPSB4OwoKICAgIGlmKGV4cG9uZW50KQogICAgewogICAgICAgIGZvcihpbnQgaSA9IDE7IGkgPCBleHBvbmVudDsgaSsrKQogICAgICAgIHsKICAgICAgICAgICAgeCA9IHggKiBiYXNlX251bTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHg7CiAgICB9CiAgICBlbHNlCiAgICB7CiAgICAgICAgcmV0dXJuIDE7CiAgICB9Cn0KCnZvaWQKSW50VG9Bc2NpaShjaGFyICpvdXRwdXRfYnVmZmVyLCBjb25zdCBpbnQgbnVtYmVyLCBjb25zdCBCYXNlcyBiYXNlKSB7CiAgICBpbnQgbnVtID0gbnVtYmVyOwogICAgaW50IHRlbXBfbnVtID0gMDsKICAgIGludCBkaWdpdCA9IDA7CiAgICBpbnQgdG90YWxfZGlnaXRzID0gMDsKICAgIGludCBkaXZpc29yID0gMDsKCiAgICBpZiAobnVtID09IDApCiAgICB7CiAgICAgICAgKm91dHB1dF9idWZmZXIgPSAnMCc7CiAgICAgICAgcmV0dXJuOwogICAgfSBlbHNlIGlmKG51bSA8IDApCiAgICB7CiAgICAgICAgKm91dHB1dF9idWZmZXIgPSAnLSc7CiAgICAgICAgb3V0cHV0X2J1ZmZlcisrOwogICAgICAgIG51bSA9IG51bSAqIC0xOwogICAgfQogICAgCiAgICB0ZW1wX251bSA9IG51bTsKICAgIHdoaWxlICh0ZW1wX251bSA+IDApIHsKICAgICAgICB0ZW1wX251bSA9IHRlbXBfbnVtIC8gYmFzZTsKICAgICAgICB0b3RhbF9kaWdpdHMrKzsKICAgIH0KCiAgICB0b3RhbF9kaWdpdHMgPSB0b3RhbF9kaWdpdHMgLSAxOwoKICAgIGZvciAoaW50IHggPSB0b3RhbF9kaWdpdHM7IHggPj0gMDsgeC0tKQogICAgewogICAgICAgIGRpdmlzb3IgPSBQb3coYmFzZSwgeCk7CiAgICAgICAgZGlnaXQgPSBudW0gLyBkaXZpc29yOwogICAgICAgIGlmKGRpZ2l0IDwgMTAgfHwgYmFzZSA9PSBCQVNFX0RFQ0lNQUwpCiAgICAgICAgewogICAgICAgICAgICAqb3V0cHV0X2J1ZmZlciA9IChjaGFyKShkaWdpdCArICcwJyk7CiAgICAgICAgfQogICAgICAgIGVsc2UgaWYoYmFzZSA9PSBCQVNFX0hFWEFERUNJTUFMKQogICAgICAgIHsKICAgICAgICAgICAgZGlnaXQgPSBkaWdpdCAtIDEwOwogICAgICAgICAgICAqb3V0cHV0X2J1ZmZlciA9IChjaGFyKShkaWdpdCArICdBJyk7CiAgICAgICAgfQoKICAgICAgICBudW0gPSBudW0gLSAoZGlnaXQgKiBkaXZpc29yKTsKICAgICAgICAKICAgICAgICBvdXRwdXRfYnVmZmVyKys7CiAgICB9Cn0KCgppbnQgbWFpbigpCnsKICAgIGNoYXIgdGV4dFsyNTZdID0ge307CiAgICBJbnRUb0FzY2lpKHRleHQsIDB4NWVhZGJlZWYsIEJBU0VfSEVYQURFQ0lNQUwpOwogICAgcHJpbnRmKCIlc1xuIiwgdGV4dCk7Cn0KCgoKCgoKCg==