Settings | Sign in | Sign up

There are currently 6 posts.

【函数】宽字节CString转char*

Floor 1 巨大八爪鱼 7/24/12 16:21

看来char*与char[]是两种不同的字符串。废话少说先上函数代码:

 

 

 

void W2C(const wchar_t* pwstr,char* pcstr,size_t len)
{
 int nlength=wcslen(pwstr);
 unsigned int nbytes=WideCharToMultiByte(0,0,pwstr,nlength,NULL,0,NULL,NULL);
 if (nbytes>len)
  nbytes=len;
 WideCharToMultiByte(0,0,pwstr,nlength,pcstr,nbytes,NULL,NULL);
}
char* CStringToChar(CString* str)
{
 char* ch=0x00; //赋初值避免警告
 size_t len=str->GetLength();
 wchar_t* wch=str->GetBuffer();
 ch=new char[len+1];
 W2C(wch,ch,len);
 ch[len]='\0';
 return ch;
}

Floor 2 巨大八爪鱼 7/24/12 16:22

用法很简单。

 

CString str=_T("dp=06DDRT&c=70");
char* ch=CStringToChar(&str);

Floor 3 巨大八爪鱼 7/24/12 16:23
————————————————————完毕————————————
Floor 4 巨大八爪鱼 7/24/12 16:24
CStringToChar函数就是_T的逆运算
Floor 5 巨大八爪鱼 11/16/14 1:09
char*与char[]转换起来很麻烦,所以不要认为这两家伙就是一个类型
Floor 6 巨大八爪鱼 6/20/16 22:49
char *和char[]的区别:
https://zh.arslanbar.net/post.php?t=24190

Content converter:

Reply the post
Content:
User: You are currently anonymous.
Captcha:
Unclear? Try another one.