cstring越界_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);
總結
以上是生活随笔為你收集整理的cstring越界_char*、WCHAR*、vc8中的CString的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 智能照明控制系统电路图_咻享智能|学校教
- 下一篇: linux下 udf提权_mysql——