How2 convert long to sring in C++ ?
long A = 12345678;
string B =A; // < won't compile obviously.
Does any1 know a function that converts long to string ?
Thanks.
------------------
There are no stupid questions, there are stupid answers.
Printable View
How2 convert long to sring in C++ ?
long A = 12345678;
string B =A; // < won't compile obviously.
Does any1 know a function that converts long to string ?
Thanks.
------------------
There are no stupid questions, there are stupid answers.
string B = (string) A;Quote:
Originally posted by IL96:
How2 convert long to sring in C++ ?
long A = 12345678;
string B =A; // < won't compile obviously.
Does any1 know a function that converts long to string ?
Thanks.
------------------
DHAHL3seasons GP:73 G:121 A:55 Pts:176 GWG:12 +/-:184
UWSWA1season GP:9 G:12 A:8 Pts:20 GWG:3 +/-:-3
uwcdc.com or namgor.com
No, you see A is not a string, it's long.Quote:
Originally posted by namgor:
string B = (string) A;
Anywhoo, i think i found a function taht sorta kinda does that, it works like this (for future reference)
int radix = 10; // base (2,8,10,16, whatever)
char buffer[33];// temp buffer
char *strptr;// pointer to an aray of characters (string)
strptr = ltoa(12345678,buffer,radix);
printf("The value is: %s\n",strptr);
------------------
There are no stupid questions, there are stupid answers.
[This message has been edited by IL96 (edited July 16, 2001).]
What he did was called a type cast.
string B = (string) A;
What this does is that it turns A into a string and then assigns its value to B.
Check in a book or online for 'Type Casts' or just 'Casts'
Namgor knows his stuff
------------------
## root is the greed of all evil ##
/* Navi Specs */
- Abit KT7-RAID
- Duron 800 @ 1GHz
- Geforce 2 GTS 32MB DDR
- 512MB Micron/Crucial SDRAM
- SB Live! 5.1 w/ Live! Drive
- Maxtor ATA100 40GB
- Pioneer 16x DVD
- HP 9300 series CD-RW
- Netgear FA311 NIC
- Win2k Server / Linux Mandrake 8.0
- Sony Multiscan 17SF
Wow, you can cast a long into a string? Man, this C++ stuff never ceases to amaze me! https://www.sharkyforums.com/images/.../2005/06/5.gif
I know there is another to cast it, (called dynamic cast??) Anyway, I believe this method can do the trick so i didnt bother look up the other method.
------------------
DHAHL3seasons GP:73 G:121 A:55 Pts:176 GWG:12 +/-:184
UWSWA1season GP:9 G:12 A:8 Pts:20 GWG:3 +/-:-3
uwcdc.com or namgor.com
string a = (string)an_int; does not work because std::string does not provide a conversion operator of type int
try this
#include <iostream>
#include <sstream>
#include <string>
int main()
{
int i = 123;
std::stringstream buf;
buf<<i;
std::string thestring = buf.str();
std::cout << thestring.c_str();
return 0;
}
sorry my first version of code was incorrect because std::string does not provide any overloaded operators for conversion so what you need is a string stream
and dynamic_cast is for casting between hirarchily related objects (eg. base to derived) using runtime type identification so it has no use here https://www.sharkyforums.com/images/.../2005/06/5.gif
BTW the includes dont have .h in them because that is the C++ style and puts the classes in the namespace "std" hence the use of std::
[This message has been edited by dighn (edited July 17, 2001).]
Thank you for the correction.
I'm just an amateur C programmer. So I only know the basics of casts and such. I don't really use them much except once in a while when I have to for functions I write or what not.
Thanks again, learn something new everyday. https://www.sharkyforums.com/images/.../2005/08/2.gif
[This message has been edited by biosx (edited July 17, 2001).]