converting an int to a string
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);
sprintf works.
int sprintf( char *buffer, const char *format, ... );
like so
char buf[5]
int value = 5555;
sprintf(buf,"%i",value);
int sprintf( char *buffer, const char *format, ... );
like so
char buf[5]
int value = 5555;
sprintf(buf,"%i",value);
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.
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 bufferchar * itoa(int, int, char *); // you supply the bufferchar * itoa(int, char *); // you supply the bufferchar * itoa(int, int); // you need to free the pointerchar * itoa(int); // you need to free the pointer
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 bufferchar * itoa(int, int, char *); // you supply the bufferchar * itoa(int, char *); // you supply the bufferchar * itoa(int, int); // you need to free the pointerchar * itoa(int); // you need to free the pointer
Hmm, didn't know that. Nevermind, then.
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;}
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?
Quote:Original post by FrunyQuote: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. :)
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(); }
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement