how to convert integer into const char * in c++
thanks
convert int into const char *
Started by mnbvlk, Jan 25 2008 07:05 AM
12 replies to this topic
Sponsor:
#3 Moderators - Reputation: 6667
Posted 25 January 2008 - 07:07 AM
You can either use boost::lexical_cast or a std::stringstream.
Once in a string, you can use the c_str() member function to get a const char *.
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 *.
#4 Members - Reputation: 112
Posted 25 January 2008 - 07:16 AM
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
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
#7 Members - Reputation: 188
Posted 25 January 2008 - 07:45 AM
A little odd, but why has no-one yet mentioned itoa()?
personally I use _itot(...).
_itoa, _i64toa, _ui64toa, _itow, _i64tow, _ui64tow (CRT)
personally I use _itot(...).
_itoa, _i64toa, _ui64toa, _itow, _i64tow, _ui64tow (CRT)
#8 Members - Reputation: 100
Posted 25 January 2008 - 07:50 AM
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
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
#9 Moderators - Reputation: 6667
Posted 25 January 2008 - 07:56 AM
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.
#12 Members - Reputation: 1900
Posted 25 January 2008 - 07:59 AM
Quote:
Original post by superdeveloper
A little odd, but why has no-one yet mentioned itoa()?
personally I use _itot(...).
Quote:@The OP: Since you're programming in C++, I would suggest following SiCrane's suggestion instead.
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
Suggested reading.
#13 Members - Reputation: 170
Posted 25 January 2008 - 03:24 PM
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






