Convert binary string to integer using stoi() function in C++ STL

C++ STL stoi() function: Here, we are going to learn how to convert a given binary string to an integer using stoi() function.
Submitted by IncludeHelp, on March 11, 2019

Given a binary string, we have to convert it into an integer using stoi() function.

C++ STL stoi() function

stoi() stands for string to integer, it is a standard library function in C++ STL, it is used to convert a given string in various formats (like binary, octal, hex or a simple number in string formatted) into an integer.

Syntax:

    int stoi (const string&  str, [size_t* idx], [int base]);

Parameters:

  • const string& str is an input string.
  • size_t* idx is an optional parameter (pointer to the object whose value is set by the function), it's default value is 0 or we can assign it to nullptr.
  • int base is also an optional parameter, its default is 10. It specifies the radix to determine the value type of input string (2 for binary, 8 for octal, 10 for decimal and 16 for hexadecimal).

Return value: It returns converted integer value.

Example:

    Input:
    string bin_string = "10101010";

    Function call:
    stoi(bin_string, 0, 2);

    Output:
    170

C++ STL code to convert a binary string into an integer

#include <iostream>
#include <string>
using namespace std;

int main()
{
	string bin_string = "10101010";
	int number =0;

	number = stoi(bin_string, 0, 2);
	cout<<"bin_string: "<<bin_string<<endl;
	cout<<"number: "<<number<<endl;

	bin_string = "111100001100111010";
	number = stoi(bin_string, 0, 2);
	cout<<"bin_string: "<<bin_string<<endl;
	cout<<"number: "<<number<<endl;

	return 0;
}

Output

bin_string: 10101010
number: 170
bin_string: 111100001100111010
number: 246586

Reference: std::stoi()




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.