Integer to string?

This is a discussion on Integer to string? within the C++ Programming forums, part of the General Programming Boards category; Ok - I'm trying to turn an integer into a string. Say I have 203. I want it to become ...

  1. #1
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379

    Integer to string?

    Ok - I'm trying to turn an integer into a string.

    Say I have 203. I want it to become "203". I cant really think of any way to do this. My first idea was disasembling the integer and take it byte by byte. But I'm not quite sure how to do that, so my next idea was just say

    Code:
    char test=2238;
    string the;
    
    the+=test;
    But it ends up as some odd letter, it probably just cycles through 255 until it finds the value. (255, 510, 765, ect)

    I looked online, it gave me some libraries, but no code to solve it. I thought the idea was simple enough - I guess not.

    So, is their an easy way to cast an integer to a string? Vice versa is much easier, as you can dissasemble a string.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,302
    You can use stringstreams with #include <sstream>
    e.g.
    Code:
    int num = 203;
    std::string str;
    std::stringstream ss;
    ss << num;
    ss >> str;
    //now str contains "203"
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Ah, thank you!
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 10:11 AM
  2. Conversion of character string to integer
    By supaben34 in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2003, 04:34 AM
  3. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 11:32 AM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21