• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer

Mr.CodeHunter

Programming and Code Solutions

  • Home
  • C++
  • About Us
  • Contact Us
  • Privacy Policy

How to convert int to string c++ ? [SOLVED]

May 28, 2021 by Admin 1 Comment

Many time during implementing c++ logic or displaying string we require to convert int to string in c++. We have given different solution for c++ int to string conversion. Here we have provided solutions using std::to_string(), using std::ostringstream and using custom function to convert int to string in c++.

1) Int to string c++ ( Using std::to_string)

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

int main()
{
	int nInput = 123;
	string sOutput;

	//Using C++ 11 int to string conversion
	sOutput = to_string(nInput);
	cout << "1) Using std::to_string():" << sOutput << endl;
	return 0;
}

Here std:to_string function will convert into to string in c++. Generally this function is used in c++ 11. Here we will get input as string value “123”.

2) convert int to string c++ (Using std::ostringstream)

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

std::string NumberToString(int Number)
{
	std::ostringstream ss;
	ss << Number;
	return ss.str();
}

int main()
{
	int nInput = 123;
	string sOutput;
    
    // c++ convert int to string using std::ostringstream
	sOutput = NumberToString(nInput);
	cout << "2) Using ostringstream:"<< sOutput << endl;
	return 0;
}

Here we are using std::ostringstream for c++ int to string conversion. For using it we will require to add header : include <sstream> .

3) C++ Int to string using custom function

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

/**
 * C++ version 0.4 std::string style "itoa":
 * Contributions from Stuart Lowe, Ray-Yuan Sheu,
 * Rodrigo de Salvo Braz, Luc Gallant, John Maloney
 * and Brian Hunt
 */
std::string CustomIntToStr(int IntValue, int nBase) 
{
	std::string sBuf;
	// check that the base if valid
	if (nBase < 2 || nBase > 16) return sBuf;

	enum { kMaxDigits = 35 };
	sBuf.reserve(kMaxDigits); // Pre-allocate enough space.

	int quotient = IntValue;

	// Translating number to string with base:
	do {
		sBuf += "0123456789abcdef"[std::abs(quotient % nBase)];
		quotient /= nBase;
	} while (quotient);

	// Append the negative sign
	if (IntValue < 0) sBuf += '-';

	std::reverse(sBuf.begin(), sBuf.end());
	return sBuf;
}

int main()
{
	int nInput = 123;
	string sOutput;
    
    // c++ int to string using cutom function
	sOutput = CustomIntToStr(nInput, 10);
	cout << "3) Using custom function:  " << sOutput <<endl;
	return 0;
}

We have used custom function which is similar to itoa(), It will be used for c++ int to string conversion.

If we are using boost library then also we can implement using including #include “boost/lexical_cast.hpp”, and then use lexical_cast<string>(Integer).

CONCLUSION:

In c++ program whenever we want to convert int to string in c++, we will require to use any of given methods. If our c++ compiler support c++11 or advance version then it will be more easy task because c++11 onward it is improving and you will find many functions in boost library as well. I hope given article will be useful for you to convert int to string c++ easily in c++ 11 or older versions as well.

Filed Under: All C++, C++, C++11

Reader Interactions

Comments

  1. Daya Shanker Prasad says

    June 21, 2021 at 5:45 pm

    This is very easy, do the same thing during compile time. Convert int to string during compilation.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Search here

Social Media

  • Facebook
  • YouTube

SEE MORE

Fibonacci sequence c++

Fibonacci Sequence c++ is a number sequence which created by sum of previous two numbers. First two number of series are 0 and 1. And then using these two number Fibonacci series is create like 0, 1, (0+1)=1, (1+1)=2, (2+1)=3, (2+3)=5 …etc Displaying Fibonacci Series in C++ ( without recursion) Output: From given output we […]

map c++

C++ Map [Learn by Example]

C++ map is part of Standard Template Library (STL). It is type of Associative container. Map in c++ is used to store unique key and it’s value in data structure. But if you want to store non-unique key value then you can use Multi Map in c++. Let us first understand in detail what is […]

how to copy paste in turbo c++ ?

There are many different C++ IDE are available but still many students are using Turbo c++ for learning c/c++ programming languages. During using Turbo c++ if you are beginner you will be confuse for how to copy and paste in turbo c++ or if you have already copy some content and you want to paste […]

C++

return 0 c++

There are two different scenario return statement is used inside c++ programming. We can use return 0 c++ inside main() function or other user defined functions also. But both have different meanings. return 0 c++ used inside Main function return 0 c++ used inside other user defined function What is meaning of return 0 and […]

C++

c++ expected a declaration [ SOLVED]

When any function or statement is not in scope or we have used wrong syntax then possibly you will get error for c++ expected a declaration in your code. Main reasons for errors are: Incorrect use/declaration inside namespace Statements are added out of scope Required statement need to add inside main/function Solution-1 | Expected a […]

C++

c++ cannot open source file “errno.h” [SOLVED]

Normally you will face c++ cannot open source file “errno.h” in MS Visual Studio c++ projects. These are some solutions to remove opening errors for “errno.h” file. I got the errors to go away by installing the Windows Universal CRT SDK component, which adds support for legacy Windows SDKs. You can install this using the […]

Footer

DISCLAIMER

The information contained on https://www.mrcodehunter.com is for general information purposes only. We assumes no responsibility for errors or omissions in the contents on the Service.

SITEMAP XML

Sitemap

Recent

  • Fibonacci sequence c++
  • C++ Map [Learn by Example]
  • how to copy paste in turbo c++ ?
  • return 0 c++
  • c++ expected a declaration [ SOLVED]

Search

Copyright © 2025 ยท Mr Code Hunter