🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Data type conversions??

Started by
8 comments, last by Someone 24 years, 5 months ago
Hi! I''m rather new to C++ (not really new, tried it out before about a year ago, but due to the ever changing programming languages that I need to study for my course, I don''t quite remember how it is done), so this might be an easy question to some... How do I convert any type of numerical data types (i.e integer, float, etc.) to a char string (char* or LPCTSTR in Visual C++)?? I know there''s a function called itoa but not too sure what is needed in the parameters. But that only converts integers. How about floats and doubles? Or better yet, anyone know how to create a printf like function, where you type things like function_name("Integer is %i, float is %f", intno, floatno) And it''ll function something like printf by replacing the %i and %f accordingly? It would be better if the function will convert the numbers to char* first though, cause I''ve already coded some things to display messages by taking an char* or LPCTSTR variable. Anyone can give a hand?
Advertisement
Sorry for the triple post... some error with the server.
One way to do it, is just use the strstream class, which works out for all primitives. i.e.:

strstream str_stream;
char * text;
str_stream << any_basic_datatype_variable;
text << str_stream;

Or else you can simply overload a new function that takes any primitive and sprintf''s it into a static buffer. i.e.:

char * ToCharString(int value) {
static char buffer[SOME_CONSTANT];
sprintf(buffer, "%i", value);
return buffer;
}

char * ToCharString(float value) {
static char buffer[SOME_CONSTANT];
sprintf(buffer, "%f", value);
return buffer;
}

and so on.

Just don''t forget the static on the char buffer.
I''d say that the stream methods SiCrane mentioned are best if you intend to do this c++ style.

Otherwise, I think sprintf matches the function_name you are looking for. It functions just like printf, but instead of printing the result to stdout, it places it in the buffer you provide.

Also, you _could_ look at the variable argument functions in the c stdlibrary, va_start, va_end, etc..

These allow you to write your own functions with variable argument lists. From your comments above, though, It sounds like sprintf would be more than sufficient, in the c style.

Notwen
Notwenwww.xbox.com
Here''s some conversion routines:

Integer to string:
itoa(int value, char* string, int radix)
(radix is the base, 10 for decimal, 16 for hex, etc)

Double to string:
ecvt( double value, int count, int *dec, int *sign );
(dec is the decimal point position)

Float to string:
fcvt( double value, int count, int *dec, int *sign );

Long to string:
ltoa( long value, char *string, int radix );

- Question #2
Why not just use sprintf and get it over with?

-fel
~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~
FYI: I''m pretty sure itoa() isn''t an ANSI function. The ClanLib source code has a comment along the lines of "why isn''t this supported in linux?"
Thanx! I''ve tried out sprintf and it works just how I wanted it. By the way, I''ve checked out on how to use va_arg, va_start, va_stop, va_list to emulate the printf function in my own functions, but in the example I''ve seem, it requires you to pass an integer with the function to tell how many parameters you''ve passed. But that isn''t required on a normal printf function... Any ideas how it is done?
Use vsprintf.

void print(char *text, ...)
{
va_list argptr;
char buffer[1024];

va_start(argptr, text);
vsprintf(buffer, text, argptr);
va_end(argptr);

// Now do something with buffer
}

Basically that will take a printf type set of parameters and you end up with buffer, which will be the string you want to use.

Regards

Starfall
With the variable argument functions, it is up to you to determine a way to know how many arguments were passed (and what type they are..)

In the examples, they use an int count because it is easy..

Printf''ish functions use a specifier string. They parse the string to determine how many parms were passed afterwards.

If there is some value that never can occur in your parms you are passing (for instance, 0), you could use that parm as a flag to stop.

The variable args stuff gives you the power...Typically, though, most people end up using vsprintf or something similar


Notwen
Notwenwww.xbox.com
Thanx for the tips! And Notwen''s right... I ended up using vsprintf as well. Makes things simplier.

This topic is closed to new replies.

Advertisement