Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Converting std strings (newbie question)

Author
Message
Michael P
18
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 22nd Apr 2008 19:14 Edited at: 22nd Apr 2008 19:14
I'm trying to convert standard strings from string to int, unsigned int and float. How is this done?
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 22nd Apr 2008 19:24
First turn these strings into char* by doing (char*)mystring.c_str(), then use atoi() to convert to int, and atof() to convert to double. If you really want float then do (float)atof()

Don't you just hate that Zotoaster guy?
Michael P
18
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 22nd Apr 2008 19:32
Thanks, a related question:
How do I add an integer/unsigned int/float onto the end of a standard string? I tried 'sBuffer += iInt;' but nothing visible gets added (sBuffer is a std string).
Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 22nd Apr 2008 19:59
'a' means (char*)
'i' means (int)
'f' means (double)
'l' means (long)

To convert from one to another, use <letter>to<letter>

Above, to convert a string to an integer, it was 'atoi' or:
'a' to 'i'

To convert back just use 'itoa' or 'ftoa' for floats and doubles.

jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 23rd Apr 2008 14:32
char buf[1024];
sprintf(buf,"My Age: %i",iAge);
dbPrint(buf);

Login to post a reply

Server time is: 2024-09-29 17:24:23
Your offset time is: 2024-09-29 17:24:23