# C++: Convert integer to string?

**URL:** <https://boards.straightdope.com/t/c-convert-integer-to-string/414334>\
**Category:** Factual Questions\
**Created:** [August 3, 2007, 8:28pm UTC](https://boards.straightdope.com/t/c-convert-integer-to-string/414334 "2007-08-03T20:28:16Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fiddle\_Peghead](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/fiddle_peghead/32/3506_2.png) [@Fiddle\_Peghead](https://boards.straightdope.com/u/Fiddle_Peghead)\
**Post date:** [August 3, 2007, 8:28pm UTC](https://boards.straightdope.com/t/c-convert-integer-to-string/414334/1 "2007-08-03T20:28:16Z")

</div>

Using only ANSI Standard C++, what is the easiest way to convert an interger to a string without using sprintf or any of its variations?

---

<div class="post-metadata">

**Author:** ![Athena](https://avatars.discourse-cdn.com/v4/letter/a/35a633/32.png) [@Athena](https://boards.straightdope.com/u/Athena)\
**Post date:** [August 3, 2007, 8:30pm UTC](https://boards.straightdope.com/t/c-convert-integer-to-string/414334/2 "2007-08-03T20:30:54Z")

</div>

\<clip\> nevermind, I read your question wrong.

---

<div class="post-metadata">

**Author:** ![Omphaloskeptic](https://avatars.discourse-cdn.com/v4/letter/o/bcef8e/32.png) [@Omphaloskeptic](https://boards.straightdope.com/u/Omphaloskeptic)\
**Post date:** [August 3, 2007, 8:33pm UTC](https://boards.straightdope.com/t/c-convert-integer-to-string/414334/3 "2007-08-03T20:33:45Z")

</div>

If you mean “without using the old C I/O libraries,” then \<stringstream\>. Or do you mean “without any external libraries”?

---

<div class="post-metadata">

**Author:** ![Capt.Ridley\_s\_Shooting\_Party](https://avatars.discourse-cdn.com/v4/letter/c/cc9497/32.png) [@Capt.Ridley\_s\_Shooting\_Party](https://boards.straightdope.com/u/Capt.Ridley_s_Shooting_Party)\
**Post date:** [August 3, 2007, 8:40pm UTC](https://boards.straightdope.com/t/c-convert-integer-to-string/414334/4 "2007-08-03T20:40:55Z")

</div>

Definitely _stringstream_:

```auto

#include <sstream>
#include <string>
#include <iostream>

int main(int argc, char** argv)
{
  int input = 5;
  std::string buffer;
  std::stringstream ss;
  ss << input;
  ss >> buffer;
  std::cout << buffer << std::endl;
  return 0;
}

```

That should work.

---

<div class="post-metadata">

**Author:** ![beowulff](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/beowulff/32/542_2.png) [@beowulff](https://boards.straightdope.com/u/beowulff)\
**Post date:** [August 3, 2007, 8:41pm UTC](https://boards.straightdope.com/t/c-convert-integer-to-string/414334/5 "2007-08-03T20:41:10Z")

</div>

[QUOTE=Fiddle Peghead]  
Using only ANSI Standard C++, what is the easiest way to convert an interger to a string without using sprintf or any of its variations?  
[/QUOTE]

I can show you how to do it in C…

---

<div class="post-metadata">

**Author:** ![DudleyGarrett](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@DudleyGarrett](https://boards.straightdope.com/u/DudleyGarrett)\
**Post date:** [August 3, 2007, 9:09pm UTC](https://boards.straightdope.com/t/c-convert-integer-to-string/414334/6 "2007-08-03T21:09:28Z")

</div>

ItoA?

---

<div class="post-metadata">

**Author:** ![Pleonast](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/pleonast/32/1183_2.png) [@Pleonast](https://boards.straightdope.com/u/Pleonast)\
**Post date:** [August 3, 2007, 9:17pm UTC](https://boards.straightdope.com/t/c-convert-integer-to-string/414334/7 "2007-08-03T21:17:21Z")

</div>

One of my favorite utility templates:

```auto

#include <sstream>
template<typename Type_t>
std::string ToStr(const Type_t& T)
 {
  std::ostringstream sout; sout<<T; return sout.str();
 }

```

Will convert any printable type into a string.

---

<div class="post-metadata">

**Author:** ![Capt.Ridley\_s\_Shooting\_Party](https://avatars.discourse-cdn.com/v4/letter/c/cc9497/32.png) [@Capt.Ridley\_s\_Shooting\_Party](https://boards.straightdope.com/u/Capt.Ridley_s_Shooting_Party)\
**Post date:** [August 3, 2007, 10:16pm UTC](https://boards.straightdope.com/t/c-convert-integer-to-string/414334/8 "2007-08-03T22:16:24Z")

</div>

[QUOTE=DudleyGarrett]  
ItoA?  
[/QUOTE]

Not typesafe, nor is it standard ANSI C (it won’t work with some gcc’s).

---

<div class="post-metadata">

**Author:** ![DudleyGarrett](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@DudleyGarrett](https://boards.straightdope.com/u/DudleyGarrett)\
**Post date:** [August 3, 2007, 10:30pm UTC](https://boards.straightdope.com/t/c-convert-integer-to-string/414334/9 "2007-08-03T22:30:57Z")

</div>

[QUOTE=Dominic Mulligan]  
Not typesafe, nor is it standard ANSI C (it won’t work with some gcc’s).  
[/QUOTE]

Ah, sorry. It’s been a while. These days I’d write it like “[int].ToString();” or “string s = Convert.ToString([int variable]);” 😉
