来讨论关于字符 如何实现以下数据类型的相互转换?CStringstringchar *char xx[SIZE]注意内存泄露 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://dev.hoyod.net/article/vc/2004-06-14/4786.htm 准确地说是相互赋值或引用,好像没有string类型CString strVal;char* lpsz;char xx[SIZE];strVal = xx; /*将字符数组赋值给CString变量*/lpsz = xx; /*引用字符数组地指针*/lpsz = strVal.GetBuffer(strVal.GetLength()); /*取CString内部缓冲区指针,用strVal.ReleaseBuffer()释放*/strncpy(xx,(LPCTSTR)strVal,SIZE-1);/*拷贝CString地字符串到xx数组*/如果在UNICODE环境下还要用MultiByteToWideChar和WideCharToMultiBtyte转换 1.char * 到 CString: char sz[10]; CString str; str.Format("%s", sz);2.CString 到 char *: Cstring str; int nLength = str.GetLength(); char* sz = new char[nLength]; sz = str.GetBuffer(0); ... str.ReleaseBuffer(); delete [] sz; CString --〉stringstr = string.c_str();CString <--stringstring.append((LPCTSTR)str)char * -> CString str = char 我要的是转换后,二者没关系了。指针不指同一个地方。CString::GetBuffer(1)应该有问题吧,返回的指针可以被控制。这样吧,aa->a bb->bchar a[128];char b[128];CString aa="aa";string bb="bb";转化后结果a[0]='a' a[1]='a' a[2]='\0' a[3]='?'...b[0]='b' b[1]='b' b[2]='\0' b[3]='?'... 不甚明白楼主的意思,可否这样: for(int i = 0; i < aa.GetLength() && i < 128; i++) a[i] = aa.GetAt(i); a[aa.GetLength()] = '\0'; int nLength = aa.GetLength();for(int i = 0; i < nLength && i < 128; i++) a[i] = aa.GetAt(i);if(nLength < 127) a[nLength] = '\0'; 对,就是酱紫的。string呢,呵呵 No have Type "string", You can named it by yourself. #include <string>using namespace std;typedef basic_string<char, char_traits<char>, allocator<char> > string;就算吧,结贴 跟随鼠标的彩色泡泡? 有谁用SallyIDE和SmartWin++做IDE? 窗口切换到后台后程序死掉?详细情况见内! LNK2001,请教 在98下如何实现“禁用软驱”?? 关于DataTimePicker控件改变字体颜色的问题!! 如何知道一个程序(exe\dll)是用什么语言编的?急啊。 再次提问:一个软件技巧的问题 MFC 操作Excel数据 紧急求救!关于GDI的API若干! 有熟悉dshow播放媒体文件的吗,大家一起讨论一下 使用CRecordSet继承类进行带变量的条件查询时,只有一个查询语句始终得不到正确的结果集,向高手请教
CString strVal;
char* lpsz;
char xx[SIZE];strVal = xx; /*将字符数组赋值给CString变量*/
lpsz = xx; /*引用字符数组地指针*/lpsz = strVal.GetBuffer(strVal.GetLength()); /*取CString内部缓冲区指针,用strVal.ReleaseBuffer()释放*/
strncpy(xx,(LPCTSTR)strVal,SIZE-1);/*拷贝CString地字符串到xx数组*/如果在UNICODE环境下还要用MultiByteToWideChar和WideCharToMultiBtyte转换
char sz[10];
CString str;
str.Format("%s", sz);
2.CString 到 char *:
Cstring str;
int nLength = str.GetLength();
char* sz = new char[nLength];
sz = str.GetBuffer(0);
...
str.ReleaseBuffer();
delete [] sz;
str = string.c_str();CString <--string
string.append((LPCTSTR)str)char * -> CString
str = char
CString::GetBuffer(1)应该有问题吧,返回的指针可以被控制。这样吧,aa->a bb->b
char a[128];
char b[128];
CString aa="aa";
string bb="bb";转化后结果
a[0]='a' a[1]='a' a[2]='\0' a[3]='?'...
b[0]='b' b[1]='b' b[2]='\0' b[3]='?'...
for(int i = 0; i < aa.GetLength() && i < 128; i++)
a[i] = aa.GetAt(i);
a[aa.GetLength()] = '\0';
for(int i = 0; i < nLength && i < 128; i++)
a[i] = aa.GetAt(i);
if(nLength < 127)
a[nLength] = '\0';
using namespace std;typedef basic_string<char, char_traits<char>, allocator<char> >
string;就算吧,结贴