variable conversion

Talk about making games.

variable conversion

Postby Thanx » Sat May 01, 2010 2:54 pm

Hello people!
I'm writing not to ask for help, but rather giving help, though it wasn't asked for. :o :wink:
I wanted to share some tricky stuff about variable conversion, because it's not always evident...

You all probably know (or if you don't, then that's why I'm writing it down) that it's easy to convert strings into other variables through atof, atoi, atol
Code: Select all
someactor.textNumber = atoi(text);

That gives someactor's text the integer atoi converted from the current actor's text...
Code: Select all
somevar = atof(text);

Saves the double/floating point value into a variable from the current actor's text...
atol isn't that important because it converts to long int from text, but we all use simple integers instead of that anyway...

The question arises, as to how to convert back to strings??? Let's say I want to put a number into a string variable!
Searching through the functions you may say there's no such function that will do the conversion for me!

Or is there?

sprintf saves the day!

Code: Select all
sprintf(text, "%i", someinteger);
sprintf(text, "%f", somefloat);

Through the variable names I've already told you how it works!
Most of you may know this, but I thought I'd post this anyway, for anybody new if they'd want to raise this question. Now it's easier for us to refer them to this post... :)
http://www.youtube.com/watch?v=XyXexDJBv58
http://www.youtube.com/watch?v=Be4__gww1xQ
These are me and playing the piano (second one with a friend/fellow student)
Hope you watch and enjoy!
User avatar
Thanx
 
Posts: 314
Joined: Sat Jan 26, 2008 10:07 pm
Location: Home sweet home! :)
Score: 24 Give a positive score

Re: variable conversion

Postby Hblade » Sat May 01, 2010 3:09 pm

Thanks :D
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest