5

我想将 aCString转换为char[]. 有人告诉我该怎么做?

我的代码是这样的:

CString strCamIP1 = _T("");
char g_acCameraip[16][17];
strCamIP1 = theApp.GetProfileString(strSection, _T("IP1"), NULL);
g_acCameraip[0] = strCamIP1;
4

12 回答 12

8

这似乎是正确的。http://msdn.microsoft.com/en-us/library/awkwbzyc.aspx

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

在您的情况下,这将是

strcpy(g_acCameraip[0], (LPCTSTR)strCamIP1);
于 2012-05-14T06:49:36.767 回答
7

MSDN网站:

// 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 是基于TCHARso 如果不使用_UNICODECStringA进行编译,或者如果你使用它进行编译,_UNICODE那么它就是CStringW.

如果CStringW转换看起来有点不同(示例也来自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;
于 2012-05-14T06:49:49.697 回答
3

如果您使用的是 ATL,则可以使用转换宏之一。CString 将数据存储为 tchar,因此您将使用 CT2A()(宏名称中的 C 代表 const):

CString from("text");

char* pStr = CT2A((LPCTSTR)from);

这些宏很聪明,如果 tchar 代表 ascii(未定义 _UNICODE),它们只是将指针传递过去,什么也不做。

下面的更多信息,在ATL 字符串转换类部分下: http ://www.369o.com/data/books/atl/index.html?page=0321159624%2Fch05.html

于 2014-09-28T20:46:09.020 回答
3

您可以使用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
于 2012-05-14T06:48:58.403 回答
0

真的必须将对象复制CString到固定char数组中吗?

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

...然后 - 稍后 - 在访问条目时,例如像这样

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

...如果需要,您可以转换它们。

于 2014-02-13T11:12:10.417 回答
0

使用 memcpy 。

char c [25];
Cstring cstr = "123";
memcpy(c,cstr,cstr.GetLength());
于 2013-03-15T05:40:45.100 回答
0

CStringA/W可以廉价且隐式地转换为const char/wchar_t *. 每当您需要 C 风格的字符串时,只需传递CString对象本身(或结果.GetString()相同)。只要字符串对象处于活动状态且未修改,指针就会保持有效。

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

如果您需要可写(非常量)缓冲区,请使用.GetBuffer()可选的最大长度参数。

如果你有CStringW但你需要const char*,反之亦然,你可以使用一个临时CStringA对象:

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

但更好的方法是拥有CStrings 数组。您可以在需要空终止字符串的任何地方使用它们,但它们也会为您管理字符串的内存。

std::vector<CString> g_acCameraip(16);
g_acCameraip[0] = theApp.GetProfileString(strSection, _T("IP1"), NULL);
于 2012-05-14T06:58:32.727 回答
0

fopen 是需要 char* 参数的函数。所以如果你有 CString 作为可用的字符串,你可以使用下面的代码。开心:) 在这里,cFDlg.GetPathName().GetString();基本上在我的代码中返回 CString。

char*pp  = (char*)cFDlg.GetPathName().GetString();
FILE *fp = ::fopen(pp,"w");
于 2015-08-11T12:06:04.580 回答
0
char strPass[256];
strcpy_s( strPass, CStringA(strCommand).GetString() );
于 2016-07-21T11:04:22.717 回答
0

这很简单

ATL CStrings允许非常简单的使用,而无需在类型之间进行大量转换。您可以最轻松地做到:

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

或在 UNICODE 环境中:

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

这个怎么运作

static_cast或 C 样式转换)将触发CString::operator LPCTSTR,因此您无需自己进行任何指针重新解释,而是依赖 ATL 代码!

这个演员表操作员的文档说:

这个有用的转换运算符提供了一种有效的方法来访问包含在CString对象中的以 null 结尾的 C 字符串。没有字符被复制;只返回一个指针。小心这个运算符。如果在获得字符指针后更改CString对象,可能会导致内存重新分配,从而使指针无效。

可修改指针

如上所述,强制转换运算符返回的指针并不意味着要修改。但是,如果您仍然需要对某些过时的 C 库使用可修改指针,则可以使用 a const_cast(如果您确定该函数不会修改指针):

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

如果您想修改指针,您将无法绕过将某种内存复制到可修改内存,如其他答案所述。

于 2016-10-19T21:29:11.480 回答
0
    CString str;
    //Do something
    char* pGTA = (LPTSTR)(LPCTSTR)str;//Now the cast

只是(LPTSTR)(LPCTSTR)。希望这是你需要的:)

于 2016-04-01T14:26:18.643 回答
0

有一个硬编码的方法..

CString a = L"This is CString!";
char *dest = (char *)malloc(a.GetLength() + 1);
// +1 because of NULL char

dest[a.GetLength()] = 0; // setting null char

char *q = (char *)a.m_pszData;
//Here we cannot access the private member..
//The address of "m_pszData" private member is stored in first DWORD of &a...
//Therefore..

int address = *((int *)&a);

char *q = (char *)address;
// Now we can access the private data!, This is the real magic of C
// Size of CString's characters is 16bit...
// in cstring '1' will be stored as 0x31 0x00 (Hex)
// Here we just want even indexed chars..

for(int i = 0;i<(a.GetLength()*2);i += 2)
  dest[i/2] = *(q+i);

// Now we can use it..
printf("%s", dest);

于 2018-09-17T10:02:19.463 回答