Sign in to follow this  
Followers 0
mnbvlk

convert int into const char *

12 posts in this topic

Using type-casting.

But before posting very evil code to do that - why are you trying to do that?
0

Share this post


Link to post
Share on other sites
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);

Once in a string, you can use the c_str() member function to get a const char *.
0

Share this post


Link to post
Share on other sites
i want to display a message box showing x-coordiante whenever left mouse click is being done
MessageBox(NULL, some const char (i suppose),"ERROR",MB_OK|MB_ICONEXCLAMATION);

so i want to convert point.x into some character array to pass it in MessageBox
arguments
0

Share this post


Link to post
Share on other sites
also typecasting does not work
(const char*)point.x displays empty space instead of a coordinate

how to display the coordinates in a messagebox
0

Share this post


Link to post
Share on other sites
Quote:
Original post by mnbvlk
also typecasting does not work
(const char*)point.x displays empty space instead of a coordinate

how to display the coordinates in a messagebox


Read SiCrane's post.
0

Share this post


Link to post
Share on other sites
Its pretty simple.

First make a char.

----------------------
char StringX[5];
----------------------
next call sprintf();
----------------------

sprintf(StringX,"%d",IntX);

-------------------------
This gets String X and fills it up with IntX. Dont mind the "%d" its just a parameter.
-------------------------

int other words, this is what you need.

int iMouseX = GetMouseX() //I guess you already got that far...

char szMouseX[5];

sprintf(szMouseX,"%d",MouseX);

MessageBox(NULL,szMouseX,"ERROR",MB_OK);

Hope this helps more then it hurts
0

Share this post


Link to post
Share on other sites
Quote:
Original post by superdeveloper
A little odd, but why has no-one yet mentioned itoa()?

itoa() is a non-standard function. In fact, it's almost militantly non-standard. I've seen at least five different function signatures for itoa() over the years:

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

Don't use itoa() if you even remotely care about portable code.
0

Share this post


Link to post
Share on other sites
Quote:
Original post by Jazonxyz
char szMouseX[5];

Congratulations, your code has a buffer overrun vulnerability.
0

Share this post


Link to post
Share on other sites
Quote:
Original post by Jazonxyz
sprintf(StringX,"%d",IntX);


The OP specified C++. Idiomatic C++ uses std::string for text.
0

Share this post


Link to post
Share on other sites
Quote:
Original post by superdeveloper
A little odd, but why has no-one yet mentioned itoa()?

personally I use _itot(...).
Quote:
Original post by Jazonxyz
Its pretty simple.

First make a char.

----------------------
char StringX[5];
----------------------
next call sprintf();
----------------------

sprintf(StringX,"%d",IntX);

-------------------------
This gets String X and fills it up with IntX. Dont mind the "%d" its just a parameter.
-------------------------

int other words, this is what you need.

int iMouseX = GetMouseX() //I guess you already got that far...

char szMouseX[5];

sprintf(szMouseX,"%d",MouseX);

MessageBox(NULL,szMouseX,"ERROR",MB_OK);

Hope this helps more then it hurts
@The OP: Since you're programming in C++, I would suggest following SiCrane's suggestion instead.

Suggested reading.
0

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
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);

Once in a string, you can use the c_str() member function to get a const char *.


i use stringstream quite frequently in most Apps, its quite nice, and really easy to use, id also recommend this
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0