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 / char* to float?

Author
Message
kklouzal
15
Years of Service
User Offline
Joined: 15th May 2009
Location: Arizona
Posted: 18th Mar 2010 22:38
Every time I think 'char* to float' I then think 'impossible' every time, I have a char* variable which holds a float value "1.5" I've tried to typecast it to a float but the compiler just tells me im dumb.. it typecasts to int but then the actual value of the int is hardly close to what it should be, am I out of luck here?

Add me to your MSN!
Cetobasilius
14
Years of Service
User Offline
Joined: 29th Dec 2009
Location: Mexico
Posted: 18th Mar 2010 22:41 Edited at: 18th Mar 2010 22:45
i had the same problem but i had a workaround... multiply your value x100 before it is a char ( if you have it in a text file just use 100 instead of 1.0f, or 150 for 1.5f, 90 for 0.9f ) then use atoi, and divide it by 100 in your game, the result will be 1.5f

hi
dark coder
22
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
kklouzal
15
Years of Service
User Offline
Joined: 15th May 2009
Location: Arizona
Posted: 18th Mar 2010 23:02 Edited at: 18th Mar 2010 23:03
Thanks for the prompt response!

I have just one problem, the value's can not be multiplied by 100x before it is a char, they are being entered into a textbox so they are already a float off the bat..

EDIT: atof says it is for a double? Can I just use it like it is a float?

Add me to your MSN!
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 18th Mar 2010 23:29
Quote: "EDIT: atof says it is for a double? Can I just use it like it is a float?"


Yes.
entomophobiac
22
Years of Service
User Offline
Joined: 1st Nov 2002
Location: United States
Posted: 19th Mar 2010 09:45
I'd suggest one of two solutions:

Create a string class with the type of functionality you need, using a char* as its core.

OR

Use the STL string class. (I.e., #include <string>.


C-style strings are more hassle than they're worth, in my opinion. Then again: if you're throwing a lot of text around, maybe it's a performance decision.
TechLord
21
Years of Service
User Offline
Joined: 19th Dec 2002
Location: TheGameDevStore.com
Posted: 19th Mar 2010 11:23
kklouzal
15
Years of Service
User Offline
Joined: 15th May 2009
Location: Arizona
Posted: 19th Mar 2010 19:08
atof() works perfectly thanks for all your helps though

Add me to your MSN!

Login to post a reply

Server time is: 2024-11-20 03:48:13
Your offset time is: 2024-11-20 03:48:13