char*、WCHAR*、vc8中的CString

在vc6中喜欢用CString,因为它用起来方便,可以容易地进行字符串赋值,复制,格式化,甚至相加,然而到了vc8中,这些操作似乎都不能用了。连CString str = "Hello World!";都会报错。

网上找了一下,原来是vc8下的应用程序默认支持unicode,采用的是宽字符集,因此一般的字符串需要加一些操作才能兼容了。CString内部即是采用wchar*来表示字符串的。如果是新建一个程序,当然可以全部用wchar,这样你的程序支持度更好。但是需要更新以前的程序时,可能就要面对char和wchar的转换了。

下面转的关于wchar与char的转换比较有用:

1.头文件中要定义宏;
#define UNICODE
#define _UNICODE

2.char转换成wchar
const char *pFilePathName = "c:\\aa.dll";
int nLen = strlen(pFilePathName) + 1;
int nwLen = MultiByteToWideChar(CP_ACP, 0, pFilePathName, nLen, NULL, 0);

TCHAR lpszFile[256];
MultiByteToWideChar(CP_ACP, 0, pFilePathName, nLen, lpszFile, nwLen);

3.wchar转换成char
char *pFilePathName;
TCHAR lpszFile[256];
_tcscpy(lpszFile, _T("c:\\aa.dll"));

int nLen = wcslen(wstr)+1;
WideCharToMultiByte(CP_ACP, 0, lpszFile, nLen, pFilePathName, 2*nLen, NULL, NULL);

另外补充一些vc8下关于wchar的常用操作:

赋字符串常量:CString str=_T("Hello World!");

字符串格式化:wsprintf(str1, "%s", str2); str1.Format(_T("%s"), str2);

字符串拷贝:wcscpy(str_dst, str_src);或wsprintf(str_dst, str_stc);

求字符串长度:int len = wcslen(str);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值