Jump to content
  • Advertisement
Sign in to follow this  
CJWR

converting an int to a string

This topic is 4821 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

is there any function to do this? i'd rather not have to write my own lol.

Share this post


Link to post
Share on other sites
Advertisement
Assuming C++, since that seems to be what you've used in your other posts, you can either use boost::lexical_cast or a std::stringstream.

std::stringstream sstr;
sstr << my_int;
std::string str1 = sstr.str();

std::string str2 = boost::lexical_cast<std::string>(my_int);

Share this post


Link to post
Share on other sites
sprintf works.

int sprintf( char *buffer, const char *format, ... );

like so
char buf[5]
int value = 5555;
sprintf(buf,"%i",value);

Share this post


Link to post
Share on other sites
Quote:
Original post by Gink
sprintf works.

int sprintf( char *buffer, const char *format, ... );

like so
char buf[5]
int value = 5555;
sprintf(buf,"%i",value);

If you're going to use C, you might as well use something like itoa.

Share this post


Link to post
Share on other sites
If you care about portable code I highly recommend avoiding itoa(). It is severly non-standard. Not only is it not part of the C standard library, but in non-standard implementations I've seen at least five different function signatures for itoa(), with different memory management conventions:

char * itoa(int, char *, int); // you supply the buffer
char * itoa(int, int, char *); // you supply the buffer
char * itoa(int, char *); // you supply the buffer
char * itoa(int, int); // you need to free the pointer
char * itoa(int); // you need to free the pointer

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
If you care about portable code I highly recommend avoiding itoa(). It is severly non-standard. Not only is it not part of the C standard library, but in non-standard implementations I've seen at least five different function signatures for itoa(), with different memory management conventions:

char * itoa(int, char *, int); // you supply the buffer
char * itoa(int, int, char *); // you supply the buffer
char * itoa(int, char *); // you supply the buffer
char * itoa(int, int); // you need to free the pointer
char * itoa(int); // you need to free the pointer

Hmm, didn't know that. Nevermind, then.

Share this post


Link to post
Share on other sites
alright, for anyone who cares about this at all, here is my little function to convert a int to a string. on vc++ 6, this works up to 999,999,999. It doesn't do any formating or anything, but all my tests show it works just fine. let me know if you see any problems with it.


string int_to_string(int num){
//cout << "number inputed = " << num << endl;

int num_size = 0; //holds the size of the number
string str; //holds the actual string we will return

//find out how large the number is
int temp = num/10; //used to find how large the number is
num_size++;
while(temp != 0){
temp = temp / 10;
num_size++;
}

//cout << "num size = " << num_size << endl;

int one_place = 0;
int div = 10; //holds the number num should be divided by
for(int i = 0; i < num_size-2; i++){
div = div * 10;
}
//cout << "div = " << div << endl << endl;

for(i = num_size; i > 0; i--){
one_place = num/div;

//add this number to the string
if(one_place == 1){
str = str + "1";
}
else if(one_place == 2){
str = str + "2";
}
else if(one_place == 3){
str = str + "3";
}
else if(one_place == 4){
str = str + "4";
}
else if(one_place == 5){
str = str + "5";
}
else if(one_place == 6){
str = str + "6";
}
else if(one_place == 7){
str = str + "7";
}
else if(one_place == 8){
str = str + "8";
}
else if(one_place == 9){
str = str + "9";
}
else{
str = str + "0";
}

//find what remains of the number
num = num - (div*one_place);

//set up div for the next pass
div = div / 10;

//output info (for testing only)
//cout << "This pass info" << endl;
//cout << "div = " << div << endl;
//cout << "num = " << num << endl;
//cout << "one_place = " << one_place << endl;

}
return str;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by CJWR
alright, for anyone who cares about this at all, here is my little function to convert a int to a string. on vc++ 6, this works up to 999,999,999. It doesn't do any formating or anything, but all my tests show it works just fine. let me know if you see any problems with it.


Well, it doesn't work with negative numbers, for starters. And, well, SiCrane told you how to do it, why not use what he said?

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
Quote:
Original post by CJWR
alright, for anyone who cares about this at all, here is my little function to convert a int to a string. on vc++ 6, this works up to 999,999,999. It doesn't do any formating or anything, but all my tests show it works just fine. let me know if you see any problems with it.


Well, it doesn't work with negative numbers, for starters. And, well, SiCrane told you how to do it, why not use what he said?


i wanted to write my own lol, but the negative thing isn't a problem for what i need this for. but it is something to work on later. :)

Share this post


Link to post
Share on other sites
Turn stuff into strings, free with your purchase of 1 or more immortality rings.


#include <sstream>
#include <string>

template <typename T> std::string toString(const T &value)
{
std::stringstream stream;
stream << value;
return stream.str();
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!