Convert ATL CString to Character Array

I want to convert a CString to char[] . Someone told me how to do this?

My code looks like this:

 CString strCamIP1 = _T(""); char g_acCameraip[16][17]; strCamIP1 = theApp.GetProfileString(strSection, _T("IP1"), NULL); g_acCameraip[0] = strCamIP1; 
+4
c ++ char visual-c ++ c-strings
source share
11 answers

It looks like the right lines; http://msdn.microsoft.com/en-us/library/awkwbzyc.aspx

 CString aCString = "A string"; char myString[256]; strcpy(myString, (LPCTSTR)aString); 

which in your case will be along the lines

 strcpy(g_acCameraip[0], (LPCTSTR)strCamIP1); 
+7
source share

From the MSDN website:

 // Convert to a char* string from CStringA string // and display the result. CStringA origa("Hello, World!"); const size_t newsizea = (origa.GetLength() + 1); char *nstringa = new char[newsizea]; strcpy_s(nstringa, newsizea, origa); cout << nstringa << " (char *)" << endl; 

CString is based on TCHAR , so if you are not compiling with _UNICODE it CStringA , or if you are compiling with _UNICODE , then this is CStringW .

In case of conversion, CStringW looks a little bit (example also from MSDN ):

 // Convert to a char* string from a wide character // CStringW string. To be safe, we allocate two bytes for each // character in the original string, including the terminating // null. const size_t newsizew = (origw.GetLength() + 1)*2; char *nstringw = new char[newsizew]; size_t convertedCharsw = 0; wcstombs_s(&convertedCharsw, nstringw, newsizew, origw, _TRUNCATE ); cout << nstringw << " (char *)" << endl; 
+7
source share

You can use wcstombs_s :

 // Convert CString to Char By Quintin Immelman. // CString DummyString; // Size Can be anything, just adjust the 100 to suit. const size_t StringSize = 100; // The number of characters in the string can be // less than String Size. Null terminating character added at end. size_t CharactersConverted = 0; char DummyToChar[StringSize]; wcstombs_s(&CharactersConverted, DummyToChar, DummyString.GetLength()+1, DummyString, _TRUNCATE); //Always Enter the length as 1 greater else //the last character is Truncated 
+3
source share

If you use ATL, you can use one of the conversion macros. CString stores the data as tchar, so you should use CT2A () (C in the macro name means const):

 CString from("text"); char* pStr = CT2A((LPCTSTR)from); 

These macros are smart if tchar represents ascii (no _UNICODE is defined), they just pass a pointer and do nothing.

See the ATL String-Conversion Classs class section below for more information: http://www.369o.com/data/books/atl/index.html?page=0321159624%2Fch05.html

+2
source share

CStringA/W cheaply and implicitly converted to const char/wchar_t * . Whenever you need a C-style string, just pass the CString object itself (or the result of .GetString() , which is the same). The pointer remains valid until the string object is alive and changed.

 strcpy(g_acCameraip[0], strCamIP1); // OR strcpy(g_acCameraip[0], strCamIP1.GetString()); 

If you need a rewritable (non-constant) buffer, use .GetBuffer() with an optional argument of maximum length.

If you have CStringW , but you need const char* and vice versa, you can use a temporary CStringA object:

 strcpy(g_acCameraip[0], CStringA(strCamIP1).GetString()); 

But a much better way is to have an array of CString s. You can use them if you need a string with a null character, but they will also manage the string memory for you.

 std::vector<CString> g_acCameraip(16); g_acCameraip[0] = theApp.GetProfileString(strSection, _T("IP1"), NULL); 
0
source share

Use memcpy.

 char c [25]; Cstring cstr = "123"; memcpy(c,cstr,cstr.GetLength()); 
0
source share

Is it really possible to copy CString objects to fixed char arrays?

 enum { COUNT=16 }; CString Cameraip[COUNT]; Cameraip[0] = theApp.GetProfileString(strSection, _T("IP1"), NULL); // add more entries... 

... and then - later - when accessing elements, for example, in this way

 for (int i=0; i<COUNT; ++i) { someOp(Cameraip[i]); // the someOp function takes const CString& } 

... you can convert them if necessary.

0
source share

fopen is a function that needs a char * parameter. so if you have a CString as an available string, you can simply use the following code. be happy :) Here is cFDlg.GetPathName().GetString(); basically returns a CString in my code.

 char*pp = (char*)cFDlg.GetPathName().GetString(); FILE *fp = ::fopen(pp,"w"); 
0
source share
  CString str; //Do something char* pGTA = (LPTSTR)(LPCTSTR)str;//Now the cast 

Only (LPTSTR) (LPCTSTR). Hope this is what you need :)

0
source share
 char strPass[256]; strcpy_s( strPass, CStringA(strCommand).GetString() ); 
0
source share

It's simple

ATL CStrings allow for very simple use without having to do many conversions between types. You can most easily do:

 CString cs = "Test"; const char* str = static_cast<LPCTSTR>(cs); 

or in a UNICODE environment:

 CString cs = "Test"; const wchar_t* str = static_cast<LPCTSTR>(cs); 

How it works

static_cast (or, conversely, C-Style cast) will call CString::operator LPCTSTR , so you do not CString::operator LPCTSTR pointer yourself, but rely on ATL code!

The documentation of this operator statement says:

This useful casting operator provides an efficient method for accessing a C string with zero completion contained in a CString object. Characters are not copied; only the pointer is returned. Be careful with this operator. If you change the CString object after receiving a pointer to a character, you can cause a reallocation of memory that invalidates the pointer.

Modifiable pointers

As mentioned above, the return pointer by the translation operator is not intended to be modified . However, if you still need to use a mutable pointer for some legacy C libraries, you can use const_cast (if you are sure that the function does not change the pointer):

 void Func(char* str) // or wchar_t* in Unicode environment { // your code here } // In your calling code: CString cs = "Test"; Func(const_cast<LPTSTR>(static_cast<LPCTSTR>(test))); // Call your function with a modifiable pointer 

If you want to change the pointer, you will not get by doing some kind of copying the memory into modifiable memory, as mentioned by other answers.

0
source share

All Articles