CString/String/int/wchar/char类型相转 CString各种类型相互转换小结

1.头文件中要定义宏;
<wbr><wbr><wbr><wbr><wbr>#define<wbr><wbr>UNICODE<br><wbr><wbr><wbr><wbr><wbr>#define<wbr><wbr>_UNICODE<br><wbr><wbr><br><strong>2.char转换成wchar<br></strong><wbr><wbr><wbr><wbr><wbr>const<wbr><wbr>char<wbr><wbr>*pFilePathName<wbr><wbr>=<wbr><wbr>"c:\\aa.dll";<br><wbr><wbr><wbr><wbr><wbr>int<wbr><wbr>nLen<wbr><wbr>=<wbr><wbr>strlen(pFilePathName)<wbr><wbr>+<wbr><wbr>1;<br><wbr><wbr><wbr><wbr><wbr>int<wbr><wbr>nwLen<wbr><wbr>=<wbr><wbr>MultiByteToWideChar(CP_ACP,<wbr><wbr>0,<wbr><wbr>pFilePathName,<wbr><wbr>nLen,<wbr><wbr>NULL,<wbr><wbr>0);<br><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr>TCHAR<wbr><wbr>lpszFile[256];<br><wbr><wbr><wbr><wbr><wbr>MultiByteToWideChar(CP_ACP,<wbr><wbr>0,<wbr><wbr>pFilePathName,<wbr><wbr>nLen,<wbr><wbr>lpszFile,<wbr><wbr>nwLen);<br><wbr><wbr><br><strong>3.wchar转换成char<br></strong><wbr><wbr><wbr><wbr><wbr><wbr><wbr>char<wbr><wbr>*pFilePathName;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>TCHAR<wbr><wbr>lpszFile[256];<br><wbr><wbr><wbr><wbr><wbr>_tcscpy(lpszFile,<wbr><wbr>_T("c:\\aa.dll"));<br><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr>int<wbr><wbr>nLen<wbr><wbr>=<wbr><wbr>wcslen(wstr)+1;<wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr>WideCharToMultiByte(CP_ACP,<wbr><wbr>0,<wbr><wbr>lpszFile,<wbr><wbr>nLen,<wbr><wbr>pFilePathName,<wbr><wbr>2*nLen,<wbr><wbr>NULL,<wbr><wbr>NULL);</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

1 CString,int,string,char*之间的转换
string 转 CString
CString.format("%s", string.c_str());
char 转 CString
CString.format("%s", char*);
char 转 string
string s(char *);
string 转 char *
char *p = string.c_str();
CString 转 string
string s(CString.GetBuffer());
1,string -> CString
CString.format("%s", string.c_str());
用c_str()确实比data()要好.
2,char -> string
string s(char *);
你的只能初始化,在不是初始化的地方最好还是用assign().
3,CString -> string
string s(CString.GetBuffer());
GetBuffer()后一定要ReleaseBuffer(),否则就没有释放缓冲区所占的空间.

《C++标准函数库》中说的
有三个函数可以将字符串的内容转换为字符数组和C—string
1.data(),返回没有”\0“的字符串数组
2,c_str(),返回有”\0“的字符串数组
3,copy()

CString互转int
将字符转换为整数,可以使用atoi、_atoi64或atol。
而将数字转换为CString变量,可以使用CString的Format函数。如
CString s;
int i = 64;
s.Format("%d", i)
Format函数的功能很强,值得你研究一下。
void CStrDlg::OnButton1()
{
// TODO: Add your control notification handler code here
CString
ss="1212.12";
int temp=atoi(ss);
CString aa;
aa.Format("%d",temp);
AfxMessageBox("var is " + aa);
}
sart.Format("%s",buf);
CString互转char*
///char * TO cstring
CString strtest;
char * charpoint;
charpoint="give string a value";
strtest=charpoint;

///cstring TO char *
charpoint=strtest.GetBuffer(strtest.GetLength());
标准C里没有string,char *==char []==string
可以用CString.Format("%s",char *)这个方法来将char *转成CString。要把CString转成char *,用操作符(LPCSTR)CString就可以了。

CString转换 char[100]
char a[100];
CString str("aaaaaa");
strncpy(a,(LPCTSTR)str,sizeof(a));
2 CString类型的转换成int
CString类型的转换成int
将字符转换为整数,可以使用atoi、_atoi64或atol。
//CString aaa = "16" ;
//int int_chage = atoi((lpcstr)aaa) ;

而将数字转换为CString变量,可以使用CString的Format函数。如
CString s;
int i = 64;
s.Format("%d", i)
Format函数的功能很强,值得你研究一下。
如果是使用char数组,也可以使用sprintf函数。
//CString ss="1212.12";
//int temp=atoi(ss);
//CString aa;
//aa.Format("%d",temp);

数字->字符串除了用CString::Format,还有FormatV、sprintf和不需要借助于Afx的itoa

3 char* 在装int
#include <stdlib.h>

int atoi(const char *nptr);
long atol(const char *nptr);
long long atoll(const char *nptr);
long long atoq(const char *nptr);

4 CString,int,string,char*之间的转换
string aa("aaa");
char *c=aa.c_str();

cannot convert from 'const char *' to 'char *'
const char *c=aa.c_str();

5 CString,int,string,char*之间的转换
string.c_str()只能转换成const char *,
要转成char *这样写:
string mngName;
char t[200]; memset(t,0,200); strcpy(t,mngName.c_str());
UpdateData(TRUE)
//比如CEDIT 控件连接的变量叫m_strEditString;
if (m_strEditString.GetLength()>=500)
{
MessageBox(_T("长度不够"_,_T(""),MB_ICONSTOP)//ERROR
}
else
{
strcpy(st,m_strEditString.GetBuffer(0));
}
//以下是转换
1、CString::GetBuffer(0);//取得CString的char*
2、int intv=atoi(CString::GetBuffer(0));//CString to Int
3、float floatv=(float)atof(CString::GetBuffer(0));//CString to float
4、long longvv=atol(CString::GetBuffer(0));//CString to Long
5、double doublev=atof(CString::GetBuffer(0));//CString to Double
6、CString::Format(_T("%d,%f,%ld,%lf,%c,%s"),int,float,long,double,char,char*)//各类型到CString
<wbr><p></p> <p style="margin-top:0px; margin-bottom:14px; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"> <strong>4.char*和CString转换</strong></p> <p style="margin-top:0px; margin-bottom:14px; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"> CString 是一种很特殊的 C++ 对象,它里面包含了三个值:一个指向某个数据缓冲区的指针、一个是该缓冲中有效的字符记数(它是不可存取的,是位于 CString 地址之下的一个隐藏区域)以及一个缓冲区长度。有效字符数的大小可以是从0到该缓冲最大长度值减1之间的任何数(因为字符串结尾有一个NULL字符)。字符记数和缓冲区长度被巧妙隐藏。</p> <p style="margin-top:0px; margin-bottom:14px; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"> (1) char*转换成CString</p> <p style="margin-top:0px; margin-bottom:14px; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px">   若将char*转换成CString,除了直接赋值外,还可使用CString::Format进行。例如:</p> <p style="margin-top:0px; margin-bottom:14px; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"> <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>char chArray[] = "Char test";<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>TCHAR * p = _T("Char test");( 或LPTSTR p = _T("Char test");)<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>CString theString = chArray;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>theString.Format(_T("%s"), chArray);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>theString = p;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></p> <p style="margin-top:0px; margin-bottom:14px; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"> (2) CString转换成char*</p> <p style="margin-top:0px; margin-bottom:14px; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px">   若将CString类转换成char*(LPSTR)类型,常常使用下列三种方法:</p> <p style="margin-top:0px; margin-bottom:14px; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px">   方法一,使用强制转换。例如:</p> <p style="margin-top:0px; margin-bottom:14px; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"> <wbr><wbr><wbr><wbr><wbr><wbr>CString theString( (_T("Char test "));<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>LPTSTR lpsz =(LPTSTR)(LPCTSTR)theString;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></p> <p style="margin-top:0px; margin-bottom:14px; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px">   方法二,使用strcpy。例如:</p> <p style="margin-top:0px; margin-bottom:14px; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"> <wbr><wbr><wbr><wbr><wbr><wbr>CString theString( (_T("Char test "));<br><wbr><wbr><wbr><wbr><wbr><wbr>LPTSTR lpsz = new TCHAR[theString.GetLength()+1];<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>_tcscpy(lpsz, theString);</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></p> <p style="margin-top:0px; margin-bottom:14px; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px">   需要说明的是,strcpy(或可移值的_tcscpy)的第二个参数是 const wchar_t* (Unicode)或const char* (ANSI),系统编译器将会自动对其进行转换。</p> <p style="margin-top:0px; margin-bottom:14px; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px">   方法三,使用CString::GetBuffer。</p> <p style="margin-top:0px; margin-bottom:14px; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"> <wbr><wbr><wbr><wbr><wbr><wbr><wbr>如果你需要修改 CString 中的内容,它有一个特殊的方法可以使用,那就是 GetBuffer,它的作用是返回一个可写的缓冲指针。如果你只是打算修改字符或者截短字符串,例如:<br><wbr><wbr><wbr><wbr><wbr><wbr>CString s(_T("Char test "));<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>LPTSTR p = s.GetBuffer();</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></p> <p style="margin-top:0px; margin-bottom:14px; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"> <wbr><wbr><wbr><wbr><wbr><wbr><wbr>LPTSTR dot = strchr(p, ''.'');</wbr></wbr></wbr></wbr></wbr></wbr></wbr></p> <p style="margin-top:0px; margin-bottom:14px; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"> <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>// 在这里添加使用p的代码</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></p> <p style="margin-top:0px; margin-bottom:14px; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"> <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>if(p != NULL)</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></p> <p style="margin-top:0px; margin-bottom:14px; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"> <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>*p = _T('\0');<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>s.ReleaseBuffer();<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>// 使用完后及时释放,以便能使用其它的CString成员函数</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></p> <p style="margin-top:0px; margin-bottom:14px; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"> <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>在 GetBuffer 和 ReleaseBuffer 之间这个范围,一定不能使用你要操作的这个缓冲的 CString 对象的任何方法。因为 ReleaseBuffer 被调用之前,该 CString 对象的完整性得不到保障。</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></p> <p style="margin-top:0px; margin-bottom:14px; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"> <wbr></wbr></p> <p style="margin-top:0px; margin-bottom:14px; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"> <strong>5.CString 转为 int</strong></p> <p style="margin-top:0px; margin-bottom:14px; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"> 将字符转换为整数,可以使用atoi、_atoi64或atol。<wbr><br><wbr><wbr>CString<wbr>aaa<wbr>=<wbr>"16"<wbr>;<br><wbr><wbr>int<wbr>int_chage<wbr>=<wbr>atoi(aaa)<wbr>;<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></p> <p style="margin-top:0px; margin-bottom:14px; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"> <wbr><wbr>得到<wbr>int_chage = 16</wbr></wbr></wbr></p> <p style="margin-top:0px; margin-bottom:14px; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"> <wbr><wbr>int<wbr>atoi(const<wbr>char<wbr>*nptr);<br><wbr><wbr>long<wbr>atol(const<wbr>char<wbr>*nptr);<br><wbr><wbr>long<wbr>long<wbr>atoll(const<wbr>char<wbr>*nptr);<br><wbr><wbr>long<wbr>long<wbr>atoq(const<wbr>char<wbr>*nptr);<br><br><br><strong>6.int 转为 CString<br></strong>而将数字转换为CString变量,可以使用CString的Format函数。如<wbr><br> CString<wbr>s;<wbr><br> int<wbr>i<wbr>=<wbr>64;<wbr><br> s.Format("%d",<wbr>i)<wbr><br><br><br>  itoa是广泛应用的非标准C语言扩展函数。由于它不是标准<a href="http://baike.baidu.com/view/4312050.htm" target="_blank" style="color:rgb(29,88,209); text-decoration:initial"><span style="color:#136ec2">C语言函数</span></a>,所以不能在所有的编译器中使</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></p> <div class="text_pic" style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px; float:right; visibility:hidden; padding-bottom:3px"> <a href="http://baike.baidu.com/albums/982195/982195.html#0%24d1e312f49d687ad67709d724" target="_blank" style="color:rgb(29,88,209); text-decoration:initial"></a> <p class="pic-info" style="margin-top:0px; margin-bottom:14px"></p> </div> <p style="margin-top:0px; margin-bottom:14px; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"> 用。但是,大多数的编译器(如Windows上的)通常在&lt;stdlib.h&gt;头文件中包含这个函数。在&lt;stdlib.h&gt;中与之有相反功能的函数是atoi。功能:把一整数转换为字符串。</p> <p style="margin-top:0px; margin-bottom:14px; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"> <br></p> <p style="margin-top:0px; margin-bottom:14px; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"> <span style="background-color:rgb(255,255,255)"><span style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px">CString与其他类型的转换</span><br style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"><span style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px">这里只是介绍了很常见方法,还有其他方法也可以实现相关的功能!</span><br style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"><span style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px; color:red">1、字符串与数的转换:<br></span><span style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px">atof(字符串-&gt;double,int,long),itoa(int-&gt;字符串),ltoa(long int-&gt;字符串)</span><br style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"><span style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px">double-&gt;CString的方法:CString::Format("%d", &amp;dX);</span><br style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"><span style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"></span><br style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"><span style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px; color:red">2、CString to char*</span><br style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"><span style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px">//经过类型强制转换,可以将CString类型转换成char*,例如:</span><br style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"><span style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px">CString cStr = "Hello,world!";</span><br style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"><span style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px">char *zStr = (char*)(LPCTSTR)cStr;</span></span><br style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px; background-color:rgb(40,85,126)"><span style="background-color:rgb(255,255,255)"><span style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px">当然,这只是最快捷的一种方法。因为CString自带一个函数可以将CString对象转换成const char*,也就是LPCTSTR。</span><br style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"><span style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"></span><br style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"><span style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px; color:red">3、char* to CString</span><br style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"><span style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px">//char*类型可以直接给CString,完成自动转换,例如:</span><br style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"><span style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px">char *zStr = "Hello,world!";</span><br style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"><span style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px">CString cStr = zStr;</span><br style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"><span style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"></span><br style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"><span style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px; color:red">4、CString to LPCSTR</span><br style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"><span style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px">//将CString转换成LPCSTR,需要获得CString的长度CString cStr=_T("Hello,world!");</span><br style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"><span style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px">int nLen = cStr.GetLength();</span><br style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"><span style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px">LPCSTR lpszBuf = cStr.GetBuffer(nLen);</span><br style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"><span style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"></span><br style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"><span style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px; color:red">5、CString to LPSTR</span><br style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"><span style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px">//这个和第4个技巧是一样的,例如:</span><br style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"><span style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px">CString cStr = _T("Hello,world!");</span><br style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"><span style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px">int nLen = str.GetLength();</span><br style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"><span style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px">LPSTR lpszBuf = str.GetBuffer(nLen); </span><br></span></p> <p style="margin-top:0px; margin-bottom:14px; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"> <br></p> <p style="margin-top:0px; margin-bottom:14px; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"> <br></p> </wbr>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值