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 / Help Converting String to Char*

Author
Message
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 30th Aug 2006 11:25
I have been researching code on forums and i want to convert a float to a char* so as i can use dbPrint()
I found this code for converting to a string using namespace which i plan on using


But i really need a way of converting that to a char* any help would be greatly appreciated.

Below is code i wush to use it in


fubar
OSX Using Happy Dude
21
Years of Service
User Offline
Joined: 21st Aug 2003
Location: At home
Posted: 30th Aug 2006 12:10
Whats wrong with using sprintf or even sprintf_s ? Or does it have to belong to the almight STD class ?

Come to the last Unofficial DBPro Convention (http://convention.logicstudios.net/)
Supplying "NO" since 1974...
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 30th Aug 2006 16:01
Slowest to fastest:

std::stringstream
sprintf
_ecvt (or one of it's partner functions, microsoft only)

Easiest to hardest:

sprintf
std::stringstream
_ecvt

If you only need to use ints, then the same applies, except use itoa instead of _ecvt

fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 30th Aug 2006 16:34
so far i have this code but it only returns 0


fubar
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 30th Aug 2006 16:46
heres the complete code still not working incase you want to see what the includes etc are it returns 0 to the dbPrint function


fubar
Miguel Melo
19
Years of Service
User Offline
Joined: 8th Aug 2005
Location:
Posted: 30th Aug 2006 20:04
That's not gonna work. If for nothing else, because you are returning myChar, which is allocatted in the stack, and which will most likely be blown away when the toString() function call returns.

It certainly will be overwritten next time you call any other function from wherever toString has just been called...

I have vague plans for World Domination
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 31st Aug 2006 14:41
This code works perfectly

<b>Extra</b>:if you get rid of the leftprecfunc and remove comment on the define leftprec it can be made for doubles etc but yu overload the pow function in math.h which i cant figure how to set it with a define but can overload as function by prefixing with type ie int in this case.



fubar
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 1st Sep 2006 00:36
If it works, then it's more by luck than judgement.

Your array is allocated on the stack. The stack is used to store parameters, local variables, return addresses etc, and every time you call a new function there is a good chance that your array contents will be overwritten.

Making this:


into this:


is marginally better in that it won't get overwritten by calling new functions, but it means that you lose the old value every time you call the function again.

Making the function either write to an array area that has been passed as a parameter, or passing back an object containing the string is a much better and safer way of doing it.



When are TCG gonna make these darned functions accept const char *

fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 4th Sep 2006 04:15
Thanks Ian, atleast i was kinda close i had allsorts of problems doing this like overloaded function calls and incorrect redirection using .c_str() etc being unfamilar with the commands but now i have a solution above this post i hope it will help many others too.

fubar

Login to post a reply

Server time is: 2024-11-19 08:24:17
Your offset time is: 2024-11-19 08:24:17