Archived

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

Converting int to a string to print on screen

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

so far in my tetris clone i have a var called score witch keeps the playes current score, now it would be nice to show the player what they have scored so when i go to put the info on the screen it''s two different types char and int... how do i turn that int to a char to display on screen ? also what are the win32 commands for changing font type and font size.... thanks you for your responses

Share this post


Link to post
Share on other sites
In C, the easiest way to put an int in a char* is to use sprintf():

char scoreBuffer[16];
int score = 0;
...
sprintf(scoreBuffer, "%d", score);

Alternatively, you can use the itoa() function.

If you want to alter the font in Win32, aside from using one of the stock fonts, you'll have to CreateFont(), specifying the properties of your desired font. Just remember that, like most things in Win32, the returned HFONT points to a resource that must be freed (in this case, with DeleteObject()).

Later,
ZE.

edit: I should also mention that in order to use a HFONT, you'll need to select it into your target HDC.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links


[edited by - zealouselixir on March 24, 2004 4:31:00 PM]

Share this post


Link to post
Share on other sites
In C:

char temp[256];
int myInt = 5;
sprintf(temp, "%i", myInt);

In C++:

std::stringstream ss;
std::string temp;
ss << int;
ss >> temp; // not sure if this is right
temp = ss.str(); // if not this should work

Share this post


Link to post
Share on other sites
Another function you can use, from the Visual C++ libraries is _snprintf(). Definition from MSDN:

int _snprintf(
char *buffer,
size_t count,
const char *format [,
argument] ...
);

You can call that function everytime the new score is calculated and it will build up a string that you specify. Just include stdio.h

If you are only interested in using simple fonts, you can use the CD3DFont class that comes with the DXSDK (this is of course assuming that you are using DirectX). When the font object is created (with a specified size), you can call a method which draws a string by taking as parameters a string (that you have already built up) and x, y coordinates for the screen position.

You can also use the ID3DXFont interface which can handle more complex fonts, but it uses GDI to draw them, so there is an obvious performance hit. Ive never used this though, so cant offer any advice.


[edited by - Mr Teatime on March 25, 2004 5:08:32 AM]

Share this post


Link to post
Share on other sites
@Mr Teatime:
Generally, avoid functions whose names start with an underscore: they're implementation specific. Use snprintf (standard) instead of _snprintf (Microsoft).

@Ructions, ZealousElixir:
I wouldn't recommend the use of itoa; it's not standard either.

[Edit: Got Ructions' name wrong.]

[edited by - Oluseyi on March 25, 2004 6:04:30 AM]

Share this post


Link to post
Share on other sites
I wasn''t recommending itoa(...). That''s why I marked it as an alternative. And while it isn''t Standard, it is implemented on every platform I''ve ever written for (which admittedly, isn''t a huge number of ''em), so I guess if it became a problem, you wouldn''t have any trouble writing your own itoa to wrap a call to sprintf. To be perfectly honest, I thought of including a note that it wasn''t Standard, but omitted it at the last minute. Should''ve known some pedantic fiend would be on my tail about it...

Later,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

Share this post


Link to post
Share on other sites
quote:
Original post by kdogg
always use snprintf over sprintf. All those string functions without the ''n'' are a segfault waiting to happen.

Unless you''re printing a single integer to a buffer that''s larger than any possible string representation of any integer in any conceivable standard implementation, in which case it''s simply efficient.

Share this post


Link to post
Share on other sites