Jump to content
  • Advertisement
Sign in to follow this  
neiluk

int to char conversion and finding screen dimensions

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

Ok two completely seperate questions but i thought id save clutter and only make one post :) 1. How can i convert an int to a char array in C++. 2. How can i find the visible screen dimensions given a certain scene depth using opengl

Share this post


Link to post
Share on other sites
Advertisement
To convert an int to a string, you can either use boost::lexical_cast or a std::stringstream.

std::stringstream sstr;
sstr << my_int;
std::string str1 = sstr.str();

std::string str2 = boost::lexical_cast<std::string>(my_int);

From there you can use the c_str() member function of the string class to get a pointer to const char.

Share this post


Link to post
Share on other sites
The above is a much neater and more OO-way of that conversion.

That said, if you are working with char arrays for some reason anyway, then you could just use sprintf() straight into the char array. Its not pretty but it is fast and simple - its not against some C++ rule or anything :)


int myNumber=10;
char myText[128];
sprintf(myText,"%d",myNumber);

Share this post


Link to post
Share on other sites
rate++ for that man :) Its been so long since I ever did that I'd totally forgotten about it *changes overcomplicated code*

Share this post


Link to post
Share on other sites
Why would you play around with dynamite when you just want to open a door?

Gee, I don't know. OP, please listen to SiCrane.

As for the second question, could you please clarify?

Share this post


Link to post
Share on other sites
Quote:
Original post by UriKiller
sprintf?? Why use so complicated function to do so simple thing?
just use itoa (myNumber, myText, 10);

If you care at all about portable code, you'll avoid itoa(). Not only is this a non-standard function, but also it is one of the more dramatically non-standard functions. I have seen at least five different function signatures for itoa()

char * itoa(int, char *, int); // you supply the buffer
char * itoa(int, int, char *); // you supply the buffer
char * itoa(int, char *); // you supply the buffer
char * itoa(int, int); // you need to free the pointer
char * itoa(int); // you need to free the pointer

Share this post


Link to post
Share on other sites
Ok cheers, to clarify on the second question, if im viewing down the z axis and (0, 0, 0) is located at the centre of the screen, given a value of z how can i find the values of x & y that correspond to the edges of the screen. Or just generally how can i find the screen size, so that when the window is changed in size i can account for this when drawing relative to the edge of the window.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!