Convert CString to ANSI string in UNICODE projects
Convert CString to ANSI string in UNICODE projects
Quick Answer: use an intermediate CStringA.
- Normally, this is not something that should be done. *It is technically unreliable, unless you can guarantee that the source CString to be converted does not contain any 2-byte characters.
- This will work fine if you are using the English language without any special 2-byte symbols or accented letters.
- This article is for educational use, and explains how it can easily be done, without relying on the USES_CONVERSION macro with W2A, or ridiculous WideCharToMultiByte API functions.
- If you are using a language that actually requires Unicode (Asian languages, etc), or if the source CString contains any 2-byte character, this cannot be done. This is because there is no ANSI equivalent of any 2-byte character.
- It is the responsibility of the programmer to ensure that the source CString does not contain any 2-byte characters.
Use intermediate CStringA (highly recommended):
- Pros: this is the easiest to use.
- Cons: you cannot specify a code page.
CString LastNameW(L"Smith");
CStringA LastNameA(LastNameW);
FunctionForAnsi(LastNameA.GetString());
- Or an even simpler example:
CString LastNameW(L"Smith");
FunctionForAnsi(CStringA(LastNameW).GetString());
Here are some other ways that either do not work or are not recommended. I list them here to document things to avoid. What not to do.
WideCharToMultiByte API function (not recommended):
- Pros: you can specify the desired code page.
- Cons: too much code to write, test, debug.
CString LastNameW(L"Smith");
int nLen =
WideCharToMultiByte(CP_ACP,
0,
(LPCWSTR)LastNameW,
-1, NULL, NULL);LPSTR lpszA =
new CHAR[nLen];WideCharToMultiByte(CP_ACP,
0,
(LPCWSTR)LastNameW,
-1, lpszA, nLen);FunctionForAnsi(lpszA);
delete[] lpszA;
// free the stringW2A ATL 3.0 macros (not recommended):
- Cons: not safe inside loops.
- Cons: you cannot specify a code page.
USES_CONVERSION;
CString LastNameW(L"Smith");
FunctionForAnsi(W2A(LastNameW.GetString()));
CW2A ATL 7.0 conversion template classes (not recommended):
- There are 3 ways you can use the CW2A template class. Only one of them is the right way.
- Cons: too difficult to remember the correct usage
- Cons: too easy to use improperly.
CString LastNameW(L"Smith");
CW2A pszA(LastNameW.GetString());
// this is the right wayFunctionForAnsi(pszA);
CString LastNameW(L"Smith");
FunctionForAnsi(CW2A(LastNameW.GetString()));
// improper usage, do not do thisCString LastNameW(L"Smith");
LPCSTR pszA =
CW2A(LastNameW.GetString());
// improper usage, do not do thisFunctionForAnsi(pszA);
(LPCSTR)(LPCTSTR) cast:
- Do not use this!
- You cannot use (LPCSTR)(LPCTSTR) to cast a CString to LPCSTR in Unicode projects.
- It does compile, but it does not properly convert the CString to an LPCSTR.
- The resulting string will either be 0 or 1 length, or filled with garbage characters of unknown length, because the cast just changes the pointer type without any conversion.
- You end up with a CHAR* pointing to a WCHAR array, a very bad thing.
CString LastName(L"Smith");
FunctionForAnsi((LPCSTR)(LPCTSTR)LastName);
// improper usage, do not to thisREF:
ATL String: What's wrong with the USES_CONVERSION macros? How to avoid using them?
Using MFC MBCS/Unicode Conversion Macros
Convert CString to ANSI string in UNICODE projects的更多相关文章
- convert \uXXXX String to Unicode Characters in Python3.x
转换\uXXXX if Python3.x: str.decode no longer exists in 3.x. that']s why Python 3.4: str : AttributeEr ...
- CString和string在unicode与非unicode下的相互转换(转)
原文转自 http://blog.csdn.net/u014303844/article/details/51397556 CString和string在unicode与非unicode下的相互转换 ...
- Convert CString to TCHAR
Quote from: http://vctipsplusplus.wordpress.com/2008/05/21/cstring-to-tchar/ CString is a very usefu ...
- CString 与 std::string 相互转化
MFC中CString 与 std::string 相互转化 CString实际是CStringT, 也就是模板类, 在UNICODE环境下,实际是CStringW, 在多字符集环境下,实际是CStr ...
- 【原创】利用typeface实现不同字体的调用显示及String转换为Unicode
最近工作用到,就写个小demo demo实现从assets中利用typeface调用不同字体,并在editText中显示出来 1.layout中创建activity_main.xml文件 布局代码如下 ...
- How to convert any valid date string to a DateTime.
DateTimeFormatInfo pattern = new DateTimeFormatInfo() { ShortDatePattern = "your date pattern&q ...
- mfc中CString转化为string的方法
LL(1)分析法实验的mfc做到最后因为CString转化为string的问题卡了一个多小时,也是惨,网上各种方法找过都不行.幸亏最后还是找到几行代码搞定了.特此mark一下. USES_CONVER ...
- 异常-----Can't convert the date to string, because it is not known which parts of the date variable are in use. Use ?date, ?time or ?datetime built-in, or ?string.\u003Cformat> or ?string(format) built-
1.错误描述 五月 27, 2014 12:07:05 上午 freemarker.log.JDK14LoggerFactory$JDK14Logger error 严重: Template proc ...
- pywinauto: 导入时遇到 "TypeError: LoadLibrary() argument 1 must be string, not unicode"
pywinauto: 导入时遇到 "TypeError: LoadLibrary() argument 1 must be string, not unicode" 经查询, 看到 ...
随机推荐
- String 字符串详解 / 常用API
String 详解 / 常用API 简介 String 是不可改变的字符串序列.String 为字符串常量 StringBuilder 与StringBuffer 均为可改变的字符串序列.为字符串变量 ...
- 【BZOJ-3123】森林 主席树 + 启发式合并
3123: [Sdoi2013]森林 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2738 Solved: 806[Submit][Status] ...
- 【对比分析五】CSS阻塞和JS阻塞
js 的阻塞特性: 所有浏览器在下载 JS 的时候,会阻止一切其他活动,比如其他资源的下载,内容的呈现等等.直到 JS 下载.解析.执行完毕后才开始继续并行下载其他资源并呈现内容.为了提高用户体验,新 ...
- Codeforces Round #360 (Div. 2) D. Remainders Game 数学
D. Remainders Game 题目连接: http://www.codeforces.com/contest/688/problem/D Description Today Pari and ...
- ASP.NET 构建高性能网站 第5篇
利用分析工具分析加载页面信息 站点的优化说到底还是站点每一个页面的优化,即使得站点的页面更快的呈现在用户的眼前.所以在此之前,我们首先来看看一个web页面的组成部分: 1. Html文件:在ASP.N ...
- .net mvc控制器传递方法到视图
很多人都是在视图里面定义方法,然后再使用.我个人也是这么干的.但是为了验证是否可以将方法从控制器传递到视图,所以做了个测试.结果真的可以.原理是利用了委托(delegate),因为委托本身就是一种类型 ...
- cocos2dx 字符串拼接
;i<;i++){ ]; sprintf(str,"%d",i); ]; strcpy(totalFilename, "game_loading") ; ...
- Android开发点点滴滴——一些基础的但实用的知识(2)
1.onItemLongClick和onItemClick事件截取 当须要同一时候获得一个listview的条目长按事件(onItemLongClick)和点击事件(onItemClick)时,仅仅须 ...
- 内核升极2.6.18 升级到 2.6.32 装systemtap 原创
系统: redhat serever 5.3 linux 2.6.18 现在要升级到 LINUX 内核 2.6.32 安装步骤: 1.下载装源代码: https://www.kernel.org/ ...
- java基础学习总结——抽象类
一.抽象类介绍