Advertisement

Interger converted to String

Started by April 23, 2002 09:36 PM
4 comments, last by Tszafran 22 years, 7 months ago
Hi, I am working on a SDL game engine and it is going very well, except one problem. I caculate my frames per second and want to convert it to a string to send to my text print out function which accepts a string for the message. I know how to convert it into a int to a char but I want to convert a int to a string. Hope you understand, can anyone help?
MSN Messenger: Thomas_Szafran@hotmail.com
What language? I''ll assume C (ISO).
int Integer = 12345;char String[12];snprintf(String,12,"%d",Integer);


Advertisement
C++

I knew about that but what about something like

int Integer = 12345;
string strInteger;

snprintf(strInteger,"%d",Integer);

I dont wanna use char bla[12], instead I was to use a string. (Yes I know strings are all chars at heart but I want a string object not a char array, kinda the same thing but I hope it makes some sense.)
MSN Messenger: Thomas_Szafran@hotmail.com
How about the following:


        int main(int argc, char *argv[]){    int  iValue;    char szTempString[1000];    // fill iValue with an integer, frame rate, value        .        .        .    string testing(itoa(iValue, szTempString, 10));    cout << testing;    return 0;}        


------

Check a Reseller's Rating Before You Purchase!

[edited by - aNonamuss on April 23, 2002 11:04:01 PM]
------Shop for the Lowest Price!Then, Check a Reseller's Rating Before You Purchase!
quote: Original post by Tszafran
Hi, I am working on a SDL game engine and it is going very well, except one problem. I caculate my frames per second and want to convert it to a string to send to my text print out function which accepts a string for the message. I know how to convert it into a int to a char but I want to convert a int to a string.

Hope you understand, can anyone help?


C++ - Use stringstream


  string getFPS(){//calculate fps in variable frames  stringstream input;  input<<frames;  return input.str();}  



Why make it simple when you can make it sooo nice and complicated?
Why make it simple when you can make it sooo nice and complicated?
C++, using the Boost library.


  #include <lexical_cast.hpp>std::string myString = "12345";int myInt = boost::lexical_cast<int>( myString );  


You write it out just like any other C++ cast.

[Questions (STFW) | GDNet Start Here | GDNet Search | Forum FAQ | Google | Asking Smart Questions ]
[Docs (RTFM) | MSDN | SGI''s STL | OpenGL | File formats]
[C++ Must Haves (RTFS) | MinGW | Boost | Loki | FLTK | SDL ]

Stolen from Magmai Kai Holmlor, who held it from Oluseyi, who was inspired by Kylotan...
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement