Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

BungoMan85

FAST way to convert any number to a *char type string

This topic is 5737 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

whats the FASTEST way to convert all numbers (int, float, double, ect) to a *char string? i know similar topics have been addressed but im really curious as to the fastest (as in most efficient in all regards) way of doing it (well in all regards except the actual length of the code, im more concerned about memory usage and processing time Bungo!

Share this post


Link to post
Share on other sites
Advertisement
For runtime libraries that support it, ftoa and itoa, probably. If they don''t, consider writing your own, or checking out newlib (do a websearch).


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites

  
char *va(char *format, ...)
{
va_list argptr;
static char string[1024];

va_start (argptr, format);
vsprintf (string, format,argptr);
va_end (argptr);

return string;
}

used as:
char b = va("%i is int, %f is float",7,10.1);

~Main

==
Colt "MainRoach" McAnlis
Programmer
www.badheat.com/sinewave

Share this post


Link to post
Share on other sites
quote:
Original post by duhroach

char *va(char *format, ...)
{
va_list argptr;
static char string[1024];

va_start (argptr, format);
vsprintf (string, format,argptr);
va_end (argptr);

return string;
}
[/source]
used as:
char b = va("%i is int, %f is float",7,10.1);

~Main

==
Colt "MainRoach" McAnlis
Programmer
www.badheat.com/sinewave


That's just full of problems waiting to creep up . Like, what if he needs to return multiple strings, it'd over-write the current string, since it's a static variable. Also, setting a char b = char* won't work, but that was just a typo I presume.

If you're just trying to convert a single variables, I'd just use sprintf.

inline void DoInt(char *str, int &t)
{
sprintf(str,"%d",t);
}
inline void DoFloat(char *str, float &t)
{
sprintf(str,"%f",t);
}

int test=10;
char MyTestString[256];
DoInt(MyTestString,test);

I don't know about quickest, but it's pretty simple, and pretty fast. I'm sure itoa, and ftoa will be faster though, since this processes the string and arguments passes, while itoa just processes the integer, and ftoa just processes the float, not reads a string .


--- Edit ---
Stupid thing put the quote tags in the wrong place .

[edited by - Ready4Dis on March 31, 2003 12:27:30 AM]

Share this post


Link to post
Share on other sites
hmmm i was definatly thinking of using templates, cause i would really like to have one function that works for int, float, double, DWORD, __int64, ect.

two questions what exactly does sprintf do? and what does inline mean? ive seen it before, dunno what it does lol. if sprintf was made for consol programs like printf is then it wont do =/ i need to RETURN the *char cause i need to use it for something else (something that isnt a consol program at all lol)

Bungo!

Share this post


Link to post
Share on other sites
Sprintf is like the C i/o functions. You can use it wherever, even though I think it was made for dos. The MSDN has some stuff about it... I can''t really explain it.

-~-The Cow of Darkness-~-

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!