convert a number to binary c++ code example
Example 1: convert binary to decimal c++ stl
string bin_string = "10101010";
int number =0;
number = stoi(bin_string, 0, 2);
Example 2: 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 3: convert int to binary string c++
std::string str = std::bitset<8>(123).to_string();