integer to binary number in c++ stl code example
Example 1: convert decimal to binary in c++
#include <iostream>
using namespace std;
void decToBinary(int n)
{
int binaryNum[32];
int i = 0;
while (n > 0) {
binaryNum[i] = n % 2;
n = n / 2;
i++;
}
for (int j = i - 1; j >= 0; j--)
cout << binaryNum[j];
}
int main()
{
int n = 17;
decToBinary(n);
return 0;
}
Example 2: convert long int to binary string c++
auto int_bits_size = 32;
auto some_integer = 123456789;
std::string str = std::bitset<int_bits_size>(some_integer).to_string();