Advertisement

Type Casting

Started by August 19, 2002 05:53 PM
7 comments, last by VoytekG 22 years, 2 months ago
Hey! Is it possible to change an integer, 365, to a char*? If so, how do you do it? I''ve tried to do it with a regular type cast but it doesn''t seem to work very well. Thanks, Voytek
http://www.liquidsoftware.cjb.netGames, apps, and more!
try itoa. here''s an example:

  #include <iostream>#include <cstdlib>// the prototype for itoa is: char* itoa(int value, char* buffer, int radix);// value is the value to convert, buffer is where the string is stored// radix is the base of the number system you want to use for the conversion// eg. 10 for base 10 (decimal), 16 for base 16(hex), 2 for base 2 (binary)int main(int argc, char *argv[]){    int num = 365;    char str[4];    itoa(num, str, 10);    cout << "num: " << num << " str: " << str << endl;    system("pause");    return 0;}  

Hope this helps!

------------------------------
BASIC programmers don''t die, they just GOSUB and don''t return.
------------------------------BASIC programmers don't die, they just GOSUB and don't return.
Advertisement
itoa(), sprintf(), etc.


The hackers must have gotten into the system through the hyperlink!!

Invader''s Realm
Thanks! Works perfectly.
One more question...
Is there any way I can put that string of numbers into a new string? I have a function that only accepts one string and I would have to put "NUMBER OF SECONDS: " with the 365. How would I do that?
Thanks,
Voytek
http://www.liquidsoftware.cjb.netGames, apps, and more!
Follow Invader X''s advice and lookup sprintf
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
learn your atof itoa atoi sprintf plenty good else this will constantly plague you
Advertisement
learn your atof itoa atoi sprintf plenty good else this will constantly plague you

there is also the stringstream route of conversion, but the former is usually less hassle
+ imho you should know the basic string functions:

strcmp (and its derivs)
strcat
strcpy

etc...

Good Luck
quote: Original post by VoytekG
Is it possible to change an integer, 365, to a char*?

To give yourself the best chance of getting a good answer on these forums, always state which programming language you are using. You wouldn''t want someone to get confused and give you an answer using PL/I, would you (or maybe you would)?

This topic is closed to new replies.

Advertisement