Desperate question-

Well, I have a stupid question about simple C/C++ language maybe, but I
still don’t know the answer, so, whatever…

The question is:

How to convert int/float/uint32 etc… type to char*/char[X] or how to
get a simple d**n string from it.

(I need it because I use SoFont function to write FPS number to the
screen - The function accepts only " const char* " -)

Thanks

char s[7];
sprintf(s, “%4.1f”, 1000.0 * frame / how_many_milliseconds);On Fri, Jun 07, 2002 at 09:26:50AM +0200, Tonci wrote:

Well, I have a stupid question about simple C/C++ language maybe, but I
still don’t know the answer, so, whatever…

The question is:

How to convert int/float/uint32 etc… type to char*/char[X] or how to
get a simple d**n string from it.

(I need it because I use SoFont function to write FPS number to the
screen - The function accepts only " const char* " -)

Thanks

sprinf is your friend:

char buffer[60];
sprintf(buffer, “FPS: %i” ,fps); //uses the same syntax as printf.> ----- Original Message -----

From: Tonci
To: SDL at libsdl.org
Sent: Friday, June 07, 2002 3:26 AM
Subject: [SDL] Desperate question-

Well, I have a stupid question about simple C/C++ language maybe, but I
still don’t know the answer, so, whatever…

The question is:

How to convert int/float/uint32 etc… type to char*/char[X] or how to get
a simple d**n string from it.

(I need it because I use SoFont function to write FPS number to the screen -
The function accepts only " const char* " -)

Thanks

char buffer[100]={0};
int fps=100;
if(snprintf(buffer, 99, “%d”,fps))
yourfunction((const char *)buffer);

This should give the tools.On Friday 07 June 2002 10:26, Tonci wrote:

Well, I have a stupid question about simple C/C++ language maybe, but I
still don’t know the answer, so, whatever…

The question is:

How to convert int/float/uint32 etc… type to char*/char[X] or how to
get a simple d**n string from it.

(I need it because I use SoFont function to write FPS number to the
screen - The function accepts only " const char* " -)

Thanks

Doesn’t the function work?
it seems to be the rigth solutionOn Saturday 08 June 2002 11:32, Sami N??t?nen wrote:

On Friday 07 June 2002 10:26, Tonci wrote:

Well, I have a stupid question about simple C/C++ language maybe, but I
still don’t know the answer, so, whatever…

The question is:

How to convert int/float/uint32 etc… type to char*/char[X] or how to
get a simple d**n string from it.

(I need it because I use SoFont function to write FPS number to the
screen - The function accepts only " const char* " -)

Thanks

char buffer[100]={0};
int fps=100;
if(snprintf(buffer, 99, “%d”,fps))
yourfunction((const char *)buffer);

This should give the tools.


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

How to convert int/float/uint32 etc… type to char*/char[X] or how to
get a simple d**n string from it.

You got a bunch of good replies to this, and I hope it helped, but I have
to be a mailing-list-mother for a moment:

In the future, please ask these sort of things elsewhere; there are
newsgroups and FAQs and websites and books that cover this very thing. We
get way too much traffic as it is on this list.

Talk about something SDL-specific, and we’ll run to your aid.

Thanks,
–ryan.

Not to continue this off-topic thread too much longer, but the length
value you give to snprintf() can actually be 100, not 99:

“snprintf and vsnprintf do not write more than size bytes
(including the trailing ‘\0’)”

Which means you don’t even have to keep track of how big you made the
string (e.g., “buffer”, above):

… snprintf(buffer, sizeof(buffer), …

Which means if later you change “char buffer[100]…” to
"char buffer[50]…", you don’t have to worry about changing the
100 (or 99) in the “snprintf()” call.

That’s pretty much as safe as you can get. :slight_smile:

-bill!On Sat, Jun 08, 2002 at 12:32:27PM +0300, Sami N??t?nen wrote:

char buffer[100]={0};
int fps=100;
if(snprintf(buffer, 99, “%d”,fps))
yourfunction((const char *)buffer);