C/C++ Int To Char*

Sage
Posts: 1,066
Joined: 2004.07
Post: #1
I understand some fundamental problems with this conversion (int == 16 bits, char == 8) but what I need is some way to convert an int into a char*. I have a function that'll print a char* string into OpenGL and I have an int framerate. I would like to somehow use this function to display my framerate over my OpenGL drawings. Any ideas?
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #2
What you need to do is *format* the int into a character buffer.

Try something like:
Code:
const int MAX_SIZE = 255;
char buf[MAX_SIZE];
snprintf( buf, MAX_SIZE, "FPS: %d", myFps );

//now, pass "buf" to your print function
Quote this message in a reply
Member
Posts: 208
Joined: 2005.04
Post: #3
sprintf or snprintf... see the man pages Wink

also, on Mac OS X, an int is 32 bits
Quote this message in a reply
Sage
Posts: 1,066
Joined: 2004.07
Post: #4
Oops on the bit part. I never could remember how many bits went into each different type.

Thanks for the tips. I didn't think of that at all. Hence why I am the beginner and you all are the teachers (though my post count is un-godly huge Smile).
Quote this message in a reply
Member
Posts: 131
Joined: 2004.10
Post: #5
Or itoa() and various flavours will do the trick seeing that sprintf does a crap load of other stuff as well.

Code:
int ival = 10
char sval[20];

_itoa(ival, sval, 10);

(Currently on a windows machine so I don't know if the underscore is required.)
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  converting text input to a c char NelsonMandella 6 4,164 Feb 4, 2010 10:58 AM
Last Post: NelsonMandella
  (unsigned) char, bitsets when using - + / * wyrmmage 7 4,825 Jul 21, 2007 04:01 PM
Last Post: wyrmmage
  Char* to double, int, and back wyrmmage 18 6,720 Jun 24, 2007 11:09 AM
Last Post: akb825
  Return char if input is char, else - int? Achithyn 2 3,027 Aug 6, 2006 03:43 AM
Last Post: backslash