RE: How can I convert integers to binary or hexadecim...
Here is my solution:
#include
<iostream>
#include
<iomanip>
#include
<sstream>
using
namespace std;
string toBinary(
unsigned int num)
{
stringstream s;
for(int i 0; i<sizeof(int)*8; ++i){
s << (num&0x0001) ;
num >> 1;
}
return s.str();
}
void
outLine(unsigned int num)
{
cout << setw(10)<< dec << num;
cout << bin: << toBinary(num);
cout << hex: << hex << setw(10) << num << endl;
}
int
main()
{
unsigned int num 0;
outLine(num);
num 1;
while(num>0) {
outLine(num);
num * 2;
}
outLine(UINT_MAX);
cout << endl;
return 0;
}
Note the while loop makes use of the fact that if you start at 1 and double the number each loop then eventually the value will return to 0. Don't do this in real code!