Sign in to follow this  
CJWR

converting an int to a string

Recommended Posts

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
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
Quote:
Original post by zealotgi
Wholly crap people. They guy is looking for a simple way to do this, not template rocket science!!!

Use sprintf()


well, i'm happy with my little function, it does all i need to do. and it allows me to get back to creating my game :).

Share this post


Link to post
Share on other sites
and here is a version that works with negatives as well.


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

//first check if the number is negative (less than 0)
if(num < 0){
num = (-num);
str = str + '-';
}
else;

//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 = 1; //holds the number num should be divided by
for(int i = 0; i < num_size-1; 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;
}




i know this is sort of reinventing the wheel, but it was an interesting little hour project.

Share this post


Link to post
Share on other sites
Quote:
Original post by smart_idiot
How about instead of all those if statements, you use str += static_cast<char>('0'+one_place)?

Edit: Seems to need a cast to work right.


well, it did work fine with any number 10 and above (up to however high ints go lol), and i just fixed it so it works on int 1-9 as well. i think it works for any number that fits into an int now. have you found a number that it wouldn't work for?

Share this post


Link to post
Share on other sites
I'm bored, how about I reinvent the wheel, too?


std::string toString(int value)
{
char buffer[32] = {0}, *out = buffer+31;

if(!value)
return "0";
else if(value >= 0) for(; value > 0; value /= 10)
*--out = '0'+value%10;
else
{
for(value *= -1; value > 0; value /= 10)
*--out = '0'+value%10;

*--out = '-';
}

return out;
}



* head explody *

[Edited by - smart_idiot on July 10, 2005 12:28:40 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by smart_idiot
I'm bored, how about I reinvent the wheel, too?

*** Source Snippet Removed ***


you win lol. much better than mine. why the heck didn't i think of that?

Share this post


Link to post
Share on other sites
Quote:
Original post by bytecoder
If you're going to use C, you might as well use something like itoa.


itoa() is not ANSIC C, so it's not guaranteed that it's implemented on every compiler.

Share this post


Link to post
Share on other sites
Quote:
Original post by CJWR
the funny thing is i now need a string to int converter. :)


String to int can be done in several ways.

The C function to do it is atoi, which is standard C and C++ (as apposed to itoa which is neither).

boost::lexical_cast is probably the easiest way in C++.

You can also use stringstreams explicitly in C++


std::stringstream sstr;
sstr << my_string;
sstr >> myint;





[smile]

Share this post


Link to post
Share on other sites
Quote:
Original post by smart_idiot
Actually, I think mine would print an empty string for 0. Oopsies.


It also dosn't support locales.

That said, I can't seem to get STLPort's locale settings working right (wcout goes AWOL as soon as I try to print out a number, even with the default locale, I think I may have misbuilt the library) and MinGW has no built in locale support, so, neither does my standard library :(.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this