• Advertisement


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

a Newbie need to convert

This topic is 6453 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

use a cast operation.

int x;
(char*) x;
x = "hey, cool!";

- DarkMage139
I can do things, things you never knew,
I can change your world if you only knew.

I can do miracles if you want me to.
Anything is possible, I'll prove it to you.

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
(Sorry I forgot my account password which is why it says I''m Anonymous Poster. My nickname is Lava.)

I don''t think casting will work in that situation. An integer and a pointer to a string are totally different.

What you really should use is the function itoa(). The syntax is:

itoa(number, string, radix)

where number is the number to convert to a string, string is the buffer that you want to put the number in, and radix is the base of the number (usually 10 for decimal.)

Here''s how you would use it:

int mynumber = 55;
char mystring[2];

itoa(mynumber, mystring, 10);

Hope that helps,
- Lava

Share this post

Link to post
Share on other sites
Another way is by using sprintf(...);


int x; // Integer value
char buffer[64]; // array of chars i.e known as a string


sprintf() is very similair to printf(). The only difference is, the output goes into a string, instead of to the screen. You must make sure that the output string which goes into buffer, is less than 64 bytes (63 chars + 1 ''\0'' NULL Terminator).


Share this post

Link to post
Share on other sites
Oh yeah, Forgot to ask, "Was that for your HighScore or Lifes?"

I used to convert numbers into strings manually instead of using sprintf / itoa. You should try it someday - It involves dividing ''/'' & moduluses ''%''

Oh yeah if you want your highscore to appear like this:

000350 etc

do this:


where ''05'' means 5 Zeros. Remember though if you want a big dirty score you may want to use %ld (long decimal), and use a ''long'' rather than ''int'' - but this all depends on whats default. With MSVC++ ''signed long int'' (32-bits) is default for ''int''. In older compilers ''signed short int'' (16-bits) is default for ''int''. And some compilers have ''unsigned char'' default for ''char'' instead of ''signed char''. So be careful

Oh yeah Remember:

// where int_val is 16-bit integer , float_val is 32-bit float

Is NOT the same as:

// The output will get messed up on the second one because the sprintf ellipis will take 4 bytes (2 from the int_val, and the first 2 from the float_val) - Hence your values will be totally Fux0red. Technically Speaking.

Anyway, enough rambling by me.


Share this post

Link to post
Share on other sites

  • Advertisement