我想知道cstring的转换方法 [英] I want to know the method of conversion of cstring
问题描述
1。我想给ListCtrl :: SetItemText提供如下参数
m_List_1100.SetItemText(i,1,SP.mac);
2.但是因为SP.mac是一个char类型变量,它会导致编译错误。
3.我想将它转换为CString类型另一个变量。
另外,我想获得关于CString的转换规则
我尝试了什么:
我无法编码样本并犯同样的错误。
1. I want to give parameter to ListCtrl::SetItemText as follows
m_List_1100.SetItemText(i, 1, SP.mac);
2. But because SP.mac is a char type variable, it causes compile error.
3. I want to it to convert to CString type another variable.
In addtion to, I want to get the conversion rules about CString
What I have tried:
I have failed to code samples and make same errors.
推荐答案
lpszText
SetItemText
的参数类型为LPCTSTR
字符串。当具有ANSI字符串(LPCSTR
)时,必须使用Unicode构建将其转换为宽字符。
可以使用 MultiByteToWideChar函数(Windows)中一个转换> [ ^ ],使用CString
类更简单,因为它提供了构造函数和赋值运算符的转换。
所以只需从ANSI字符串创建CString
并传递:
ThelpszText
argument ofSetItemText
is of typeLPCTSTR
string. When having an ANSI string (LPCSTR
) it must be converted to wide char with Unicode builds.
While this can be converted using MultiByteToWideChar function (Windows)[^], using theCString
class is simpler because that provides such conversions with constructors and assignment operators.
So simply create aCString
from the ANSI string and pass that:
CString strMac(SP.mac);
m_List_1100.SetItemText(i, 1, strMac.GetString());
要了解转换规则,请参阅 CStringT Class [ ^ ]。
它使用一些预定义类型来处理不同类型的参数(参见上面链接中的 CStringT预定义类型)。有趣的转换类型是那些 Y ( YCHAR
, PYSTR
, PCYSTR
)。它们表示支持与构建设置不匹配的字符( char
, LPSTR
, LPCSTR
用于Unicode构建)。因此,当成员函数支持其中一种类型时,您可以使用Unicode构建传递ANSI字符或字符串。例如,参见 CStringT :: operator = [ ^ ]和 CStringT :: CStringT [ ^ ]构造函数。
To know about the conversion rules see the CStringT Class[^].
It uses some predefined types to handle parameters of different types (see CStringT Predefined Types in the above link). The interesting types for conversion are those with Y (YCHAR
, PYSTR
, PCYSTR
). They indicate support for characters not matching the build setting (char
, LPSTR
, LPCSTR
for Unicode builds). So when a member function supports one of these types, you can pass an ANSI char or string with Unicode builds. See for example the CStringT::operator =[^] and the CStringT::CStringT[^] constructor.
这篇关于我想知道cstring的转换方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!