Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I convert a CString to char ?

Status
Not open for further replies.

youssef

Programmer
Mar 13, 2001
96
0
0
BE
Hi,

How can I convert a CString to char ?

I have a editbox (m_editbox CString) to receive a data for exemple "ABC123". I would like to put this data into a array. But the array accept only char.

ex.:

UpdateData(TRUE);
char tc[] = m_editbox;

Is it correct ?

Best regards

 
If that doesn't work directly (and I don't think it will) you can do this:
UpdateData(TRUE);
char tc[] = (LPCTSTR)m_editbox;

The LPCTSTR operator is defined by the CString class, and provides a pointer to the data. (This DOES NOT make a copy though, so be careful!)
 
I receive a error message :
error C2440: 'initializing' : cannot convert from 'const char *' to 'char []'
There are no conversions to array types, although there are conversions to references or pointers to arrays
 
you should'n use char[]. Try to use char*. char[] is a fixed array. John Fill
1c.bmp


ivfmd@mail.md
 
In my exemple how can I do ?

Thanks a lot for your helping.
 
I think, you should use const char* or char* instead of char[]. John Fill
1c.bmp


ivfmd@mail.md
 
My apply is : convert a CString from a editbox or a database to ASCII

If I use my little program all working

UpdateData(TRUE);
//char msg[] = (LPSTR) m_text;
char msg[9] = "12345678";
int conv[8];
int c = 0;
char *p;


for( p = msg ; p < msg + strlen( msg ) ; p++ && c++ )
{
conv[c] = __toascii( *p );
}

m_text1 = conv[0]; first caracter convert in ASCII hex
m_text2 = conv[1];
m_text3 = conv[2];
m_text4 = conv[3];
m_text5 = conv[4];
m_text6 = conv[5];
m_text7 = conv[6];
m_text8 = conv[7]; the last

UpdateData(FALSE);


But when I would like to use the editbox (m_text) error message




}
 
Excuse me
When I use in my program a constant char, all are ok.
But when I would like to use ; char msg[] = (LPCTSTR)m_text;
the error message C2440 appear.


UpdateData(TRUE); for capture the text from the editbox
//char msg[] = (LPSTR) m_text;
char msg[9] = &quot;12345678&quot;; the string for convert in ascii code
int conv[8]; recover the ascii code from each caracter in the string
int c = 0; counter
char *p;


for( p = msg ; p < msg + strlen( msg ) ; p++ && c++ )
{
conv[c] = __toascii( *p );
}

m_text1 = conv[0]; first caracter convert in ASCII code
m_text2 = conv[1];
m_text3 = conv[2];
m_text4 = conv[3];
m_text5 = conv[4];
m_text6 = conv[5];
m_text7 = conv[6];
m_text8 = conv[7]; the last

UpdateData(FALSE);


 
char[] means a fixed zone of memory. There you can change anything, but you can't realloc or make it to point in other place than where it is initialized.
const char* means a pointer. This pointer you can make to point everywhere, byt you can't change with it toe memory. CString requires the const char* because it makes you to see private members. Because of it you can't change CString members throug const char*.
char[] you can't make to see at other places. In it you can write only using by example strcpy. To try to make an array to point in other places is a nonsense. John Fill
1c.bmp


ivfmd@mail.md
 
The solution for my problem is find by Philip Nicoletti and is :

CString str = &quot;This is a test&quot;;
char c0 = str.GetAt(0); // c0 = 'T'
char c1 = str.GetAt(1); // c1 = 'h'

best regards
 
To make a copy string of CString to char* you make write this:

CString s;
// ...
char* lpsz = new char[s.GetLength()+1];
strcpy(lpsz, static_cast<LPCTSTR>(s));
// ...
delete[] lpsz;

With best regards,
zaki-maksyutov@yandex.ru
 
Hello,
I am new on this, and I am looking for same solution but using standard C++ string class. f.e. :

string st1 =&quot;a string&quot;;

I need to convert this to a char * variable.

thanks in advance for your help!

Ekeko
 
You can simply use c_str() member like this
std::string aSTDString;
// assign value to aSTDString
char* aCharPtr=aSTDString.c_str()

c_str() return the pointer the wrappered C style string.

 
////////////////////////////////////////////////////
You can also convert CString into ascii char string
in following way:

CString str=CString(&quot;India is my Country&quot;);
char *pStr=new char[str.GetLength()];
strcpy(pStr,str.GetBuffer(str.GetLength()-1);
//Usage of pStr//
delete[] pStr;
////////////////////////////////////////////////////


 

Here's the easiest way...


CString test(&quot;what up&quot;);
char cTest[50];

strcpy(cTest,test.operator LPCTSTR());
 
You can try the following code to convert CString to char,
Hope this will help.

CString str;
LPTSTR lpsz = new TCHAR[str.GetLength()];
_tcscpy(lpsz, str);

regards from kelvinlee
 
This is a resolved thread. See begin date. By the way, kelvinlee, too much code. Class CString has many operators and functions, what are much easier than using workaround with C function. Ion Filipski
1c.bmp


filipski@excite.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top