String to char*
-
Hey dudes! I am using VS 2005 and doing work in Visual C++ windows Applications. I am facing a prob that i have a class in which my data members are char * but when i get data from text box or edit box it is in the form of String. But i have to convert it into char*. How can i do it?? I got how to convert char * to string by COnvert::ToString(char*). And other one problem is that when i add a class, when i add data member as String or String^ it didn't allow me. To do it which header file Should i include??
-
Hey dudes! I am using VS 2005 and doing work in Visual C++ windows Applications. I am facing a prob that i have a class in which my data members are char * but when i get data from text box or edit box it is in the form of String. But i have to convert it into char*. How can i do it?? I got how to convert char * to string by COnvert::ToString(char*). And other one problem is that when i add a class, when i add data member as String or String^ it didn't allow me. To do it which header file Should i include??
-
Hey dudes! I am using VS 2005 and doing work in Visual C++ windows Applications. I am facing a prob that i have a class in which my data members are char * but when i get data from text box or edit box it is in the form of String. But i have to convert it into char*. How can i do it?? I got how to convert char * to string by COnvert::ToString(char*). And other one problem is that when i add a class, when i add data member as String or String^ it didn't allow me. To do it which header file Should i include??
Hi, You cant use c_str() in CString. You have to use this function to convert a CString to a char *
char\* ConvertToChar(const CString &s) { int nSize = s.GetLength(); char \*pAnsiString = new char\[nSize+1\]; memset(pAnsiString,0,nSize+1); wcstombs(pAnsiString, s, nSize+1); return pAnsiString; }
All the best...
The price of anything is the amount of life you exchange for it. Thanks and Regards. SANTHOSH V
-
Hey dudes! I am using VS 2005 and doing work in Visual C++ windows Applications. I am facing a prob that i have a class in which my data members are char * but when i get data from text box or edit box it is in the form of String. But i have to convert it into char*. How can i do it?? I got how to convert char * to string by COnvert::ToString(char*). And other one problem is that when i add a class, when i add data member as String or String^ it didn't allow me. To do it which header file Should i include??
if you are using CString there is a method called GetBuffer() CString s; char *ch = s.GetBuffer(s.GetLength()) May be it will solve your problem. plz feedback if you solve it.
-
if you are using CString there is a method called GetBuffer() CString s; char *ch = s.GetBuffer(s.GetLength()) May be it will solve your problem. plz feedback if you solve it.
onlyjaypatel wrote:
if you are using CString there is a method called GetBuffer()
:omg: What ? GetBuffer is the worst thing you can call to do that. CString provides an operator to do the casting, so if it doesn't work, GetBuffer won't work neither. Also, if you do not call ReleaseBuffer later, your CString object can be corrupted. Please, read this[^].
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++ -
Hey dudes! I am using VS 2005 and doing work in Visual C++ windows Applications. I am facing a prob that i have a class in which my data members are char * but when i get data from text box or edit box it is in the form of String. But i have to convert it into char*. How can i do it?? I got how to convert char * to string by COnvert::ToString(char*). And other one problem is that when i add a class, when i add data member as String or String^ it didn't allow me. To do it which header file Should i include??
Which kind of string are you talking about ? CString (from the MFC) or std::string (from the STL) ? You probably have UNICODE define, that's probably why you can't 'convert'. I highly suggest that you read this excellent article[^], things will be much clearer afterwards.
Shirani wrote:
when i add data member as String or String^
Are you using managed C++ ? If yes, then this is the wrong forum. This forum is for C++ only, not managed.
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++ -
Hey dudes! I am using VS 2005 and doing work in Visual C++ windows Applications. I am facing a prob that i have a class in which my data members are char * but when i get data from text box or edit box it is in the form of String. But i have to convert it into char*. How can i do it?? I got how to convert char * to string by COnvert::ToString(char*). And other one problem is that when i add a class, when i add data member as String or String^ it didn't allow me. To do it which header file Should i include??
this is managed c++ related query and it should be asked in it particular section!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
Never mind - my own stupidity is the source of every "problem" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You/xml>
-
To convert String to char*, use the function c_str(). This function will return a const pointer to a regular C string(which will be identical to the passed in string). std::string Test1 = "Text to Test"; char *Test = Test1.c_str(); :cool: Regards, Rane
as per my knowledge System::String doesn't have c_str() function , though std::string generally have.. also it return const char*
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
Never mind - my own stupidity is the source of every "problem" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You/xml>
-
Which kind of string are you talking about ? CString (from the MFC) or std::string (from the STL) ? You probably have UNICODE define, that's probably why you can't 'convert'. I highly suggest that you read this excellent article[^], things will be much clearer afterwards.
Shirani wrote:
when i add data member as String or String^
Are you using managed C++ ? If yes, then this is the wrong forum. This forum is for C++ only, not managed.
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++Cedric Moonen wrote:
Are you using managed C++ ? If yes, then this is the wrong forum. This forum is for C++ only, not managed.
atleast somebody have sharp common sense here [:)]
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
Never mind - my own stupidity is the source of every "problem" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You/xml>