Reply
 
Thread Tools Display Modes
  #1  
Old 08-03-2007, 03:28 PM
Fiddle Peghead Fiddle Peghead is offline
Guest
 
Join Date: Mar 2001
Location: Beautiful downtown Newark
Posts: 3,329
C++: Convert integer to string?

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?
  #2  
Old 08-03-2007, 03:30 PM
Athena Athena is offline
Charter Member
 
Join Date: May 1999
Location: da UP, eh
Posts: 13,148
<clip> nevermind, I read your question wrong.

Last edited by Athena; 08-03-2007 at 03:31 PM.
  #3  
Old 08-03-2007, 03:33 PM
Omphaloskeptic Omphaloskeptic is offline
Guest
 
Join Date: Oct 2001
Posts: 1,263
If you mean "without using the old C I/O libraries," then <stringstream>. Or do you mean "without any external libraries"?
  #4  
Old 08-03-2007, 03:40 PM
Capt. Ridley's Shooting Party Capt. Ridley's Shooting Party is offline
Guest
 
Join Date: Jul 2003
Location: Cambridge
Posts: 6,223
Definitely stringstream:

Code:
#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.
  #5  
Old 08-03-2007, 03:41 PM
beowulff beowulff is online now
Member
 
Join Date: May 2001
Location: Scottsdale, more-or-less
Posts: 15,267
Quote:
Originally Posted by 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?
I can show you how to do it in C...
  #6  
Old 08-03-2007, 04:09 PM
DudleyGarrett DudleyGarrett is offline
Guest
 
Join Date: Apr 2007
Location: Northern Virginia
Posts: 2,187
ItoA?
  #7  
Old 08-03-2007, 04:17 PM
Pleonast Pleonast is offline
Charter Member
 
Join Date: Aug 1999
Location: Los 'Kamala'ngeles
Posts: 6,516
One of my favorite utility templates:
Code:
#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.
  #8  
Old 08-03-2007, 05:16 PM
Capt. Ridley's Shooting Party Capt. Ridley's Shooting Party is offline
Guest
 
Join Date: Jul 2003
Location: Cambridge
Posts: 6,223
Quote:
Originally Posted by DudleyGarrett
ItoA?
Not typesafe, nor is it standard ANSI C (it won't work with some gcc's).
  #9  
Old 08-03-2007, 05:30 PM
DudleyGarrett DudleyGarrett is offline
Guest
 
Join Date: Apr 2007
Location: Northern Virginia
Posts: 2,187
Quote:
Originally Posted by Dominic Mulligan
Not typesafe, nor is it standard ANSI C (it won't work with some gcc's).
Ah, sorry. It's been a while. These days I'd write it like "[int].ToString();" or "string s = Convert.ToString([int variable]);"
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 10:58 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2018, vBulletin Solutions, Inc.

Send questions for Cecil Adams to: cecil@straightdope.com

Send comments about this website to: webmaster@straightdope.com

Terms of Use / Privacy Policy

Advertise on the Straight Dope!
(Your direct line to thousands of the smartest, hippest people on the planet, plus a few total dipsticks.)

Publishers - interested in subscribing to the Straight Dope?
Write to: sdsubscriptions@chicagoreader.com.

Copyright 2018 STM Reader, LLC.

 
Copyright © 2017