Here I provide a recursive solution.
I also used argv to allow multiple inputs on command line, and each input can be either decimal-text or hex-text.
I use stringstream, rather than cin. Note, they both inherit the same stream input operations.
#include <iostream>
using std::cout, std::cerr, std::endl, std::hex, std::dec;
#include <string>
using std::string;
#include <sstream>
using std::stringstream, std::istringstream;
#include <cstdint>
// typical output vvvvv v v v v v
// n: 7340032 0x700000 11100000000000000000000
// commands -- both the same input value
// ./dumy899d 7340032
// ./dumy899d 0x700000
//
// which can be mixed in the same argv list:
// ./dumy899d 7340032 0x700000
// recursion max depth: bit width of n (32 on my system)
void to_binary_R (string& retVal, int n)
{
if (0 == n) return; // recursion termination clause
to_binary_R (retVal, n/2); // (n / 2) shifts all bits of n to right
// n is on the stack and will be unchanged when the PC returns,
// thus, during 'decurse':
int a = (n % 2) + '0'; // do computation, a is 0x30 or 0x31
// int (0 or 1) 0x30 -- char is auto promoted to int
retVal += static_cast<char>(a); // append-to-retVal
// no-code-cast: 30, 31 become chars '0', '1' with cast
}
// i sometimes prefer to return a string
// (this can enable reducing cout-pollution)
string to_binary(int n)
{
string s; s.reserve(64); // declare & reserve space
to_binary_R(s, n); // fill
return s;
}
// forward declartions
// returns true when user input has '0x'
bool hexUI(const string& s);
void usage();
int main(int argc, char* argv[])
{
if (argc < 2) { usage(); return -1; }
for (int indx = 1; indx < argc; ++indx)
{
char* arg = argv[indx]; // instead of std::cin >> n;
stringstream ss(arg);
int nOrig = 0;
if (hexUI(arg)) { ss >> hex >> nOrig; }
else { ss >> dec >> nOrig; }
if (!ss.good() && !ss.eof()) // when not good state that is not
{ // caused by eof()
// notify user:
cerr << "\n\n ERR: ss is not good() (and not eof) "
<< "Maybe a hex parameter prefix is missing '0x'?\n"
<< endl;
return -2;
}
cout << "\n n: " << dec << nOrig
<< " 0x" << hex << nOrig
<< " " << to_binary(nOrig)
<< endl;
} // for (int indx = 1; indx < argc; ++indx)
return 0;
} // int main()
// returns true when user input specifies hex with '0x' prefix
bool hexUI(const string& s) { return (s.find("0x") != string::npos); }
void usage()
{
cerr << "\n err: expecting 1 or more command line arguments, each argument is "
<< "\n either decimal-text or hex-text (marked with '0x' prefix) "
<< "\n values are displayed in decimal, binary, and hex"
<< endl;
cout << "\n The following info might be different on your system:"
<< "\n My development system: Linux 20.04, using g++ v9.3.0";
cout << "\n sizeof(int) : "
<< sizeof(int) << " bytes " // 4 bytes
<< (sizeof(int) * 8) << " bits"; // 32 bits
cout << "\n sizeof(long int) : "
<< sizeof(long int) << " bytes " // 8 bytes
<< (sizeof(long int) * 8) << " bits"; // 64 bits
// consider specifying the size, such as
cout << "\n sizeof(int64_t) : "
<< sizeof(int64_t) << " bytes " // 8 bytes
<< (sizeof(int64_t) * 8) << " bits" // 64 bits
<< endl;
}