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

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

Given an octal 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.


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


  • 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.


    string oct_string = "12345";

    Function call:
    stoi(oct_string, 0, 8);


C++ STL code to convert an octal string into an integer

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

int main()
	string oct_string = "12345670";
	int number =0;

	number = stoi(oct_string, 0, 8);
	cout<<"oct_string: "<<oct_string<<endl;
	cout<<"number: "<<number<<endl;

	oct_string = "12345";
	number = stoi(oct_string, 0, 8);
	cout<<"oct_string: "<<oct_string<<endl;
	cout<<"number: "<<number<<endl;

	return 0;


oct_string: 12345670
number: 2739128
oct_string: 12345
number: 5349

Reference: std::stoi()

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.