convert binary string to integer c++ code example

Example 1: convert binary to decimal c++ stl

string bin_string = "10101010";
int number =0;
number = stoi(bin_string, 0, 2);
// number = 170

Example 2: convert int to binary string c++

std::string str = std::bitset<8>(123).to_string();

Example 3: convert long int to binary string c++

auto int_bits_size = 32; // maximum number of bits for the integer
auto some_integer = 123456789;
std::string str = std::bitset<int_bits_size>(some_integer).to_string();

Tags:

Cpp Example