Click to See Complete Forum and Search --> : Converting an int to a string in C++
dunno
01-27-2001, 06:39 PM
I would like to convert an int to a string
I tried:
int mynumber = 3;
string SMyNumber = (char*)mynumber;
that compiles, but seg faults when run.
dotzourb
01-27-2001, 11:22 PM
Are you doing this for just a single digit, or something like 23432? If the former, then you should just do this:
int my_int = 3;
string my_str = '0' + my_int; // that's a zero
Otherwise, the casting approach will not work, you'll have to write a function to do it for you. This is an intro CS classic homework assignment! Enjoy! http://www.linuxnewbie.org/ubb/smile.gif
jemfinch
01-28-2001, 12:54 AM
In C, I would use sprintf().
Jeremy
Stuka
01-28-2001, 01:16 AM
In C++ the string class has MANY methods that allow you to add to/insert in the string. Sorta makes sprintf() superfluous (only in C++, of course! http://www.linuxnewbie.org/ubb/smile.gif)
dunno
01-28-2001, 09:54 AM
I would like a method that works for multiple or single digit numbers.
--new:
Oh well, screw it. I just decided to use sprintf();
int myint = 3;
char intString[30];
sprintf(intString, "%d", 3);
string sMyInt = intString;
Any problems seen with that code, i'm great at making mistakes?
[This message has been edited by dunno (edited 28 January 2001).]
Sterling
01-28-2001, 11:44 AM
Yeah. By default, if told to convert the number 0, sprintf converts it to a blank string. One COULD have a kludgy workaround for it like so:
char *foo[50];
int bar;
sprintf(foo, "%d", bar);
if (strcmp(foo, "") == 0)
{
foo[0] = '0';
foo[1] = '\0';
}
string sMyInt = foo;
You can also look into the strstream, as that may allow you to avoid using a c string between the integer and the C++ string.
------------------
-Sterling
"There is no Linuxnewbie.org cabal..."
f'lar
01-29-2001, 05:05 PM
Do you mean a string that looks like this:
"3"
or this:
"three"
?
------------------
If life were fair, we'd all be in Hell already.
Stuka
01-29-2001, 07:06 PM
Hmmm...how to convert arbitrary length int into a string....
int myInt, power, temp;
string myString;
power = 0;
do {
power ++;
} while (pow(10,power) <= myInt)
do{
temp = myInt/pow(10,power);
myString += (temp + '0');
myInt -= temp * pow(10,power);
power--;
}while (power >= 0);
I just threw this together, but tinkering should make it work fine.
nanode
01-30-2001, 11:15 AM
int my_int = 3;
string my_str = '0' + my_int; // that's a zero
Java works much the same:
int my_int = 3;
String my_str = new String("" + my_int);
the double quotes inside the String constructor make qualify it as a String. Bare in mind I'm talking about String class, not a C-Style string or char array.
ndogg
01-30-2001, 12:12 PM
Use ostrstream (http://www.iamexwi.unibe.ch/system/suncompiler/stdlib/stdref/str_6184.htm). The link is actually a link to strstream, but it has a link to ostrstream, but you should get the idea of how it works from this.
------------------
Too much Sun can give you cancer. Windows break too easily.
Apples/Macintoshes can rot. BSD... sounds too much like LSD.
Penguins are the only animals sophisticated enough to wear a
tuxedo.
Linux, the only one with the Penguin.
http://ndogg.n3.net
Stuka
01-30-2001, 05:13 PM
ndogg,
The problem w/ostrstream is that it expects ASCII type inputs. That works fine w/single digits, but when you throw a multi-digit integer at it, you've completely lost the functionality. You really need a function of some sort to convert, which is why I did what I did.