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" 经查询, 看到 ...
随机推荐
- virtualenv虚拟环境安装不同版本的django
在开发Python应用程序的时候,系统安装的Python3只有一个版本:3.4.所有第三方的包都会被pip安装到Python3的site-packages目录下. 如果我们要同时开发多个应用程序,那这 ...
- MySQL 语句分析
公司使用的数据库是 MySQL 数据库,我对于 MySQL 的了解仅仅是上学的时候学过PHP略微了解. 我认为,作为一个后端程序员,除了在意功能能不能实现之外, 在实现功能之后,还要去想有没有更好的办 ...
- 20162318 2018-2019-2《网络对抗技术》Exp1 PC平台逆向破解
一.实验目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. 该程序同时包含另一个代码片段,getS ...
- bzoj 1492
这道题真好... 首先,感觉像DP,但是如果按照原题意,有无数个状态,每个状态又有无数个转移. 然后思考,我们每次买一部分和卖一部分的原因是什么,如果没有那个比例(就是rate=1恒成立),那么很容易 ...
- TSQL update 简单应用小总结
UPDATE 有两种基本的格式.一种是用静态数据来修改表,另一种是用其他表中的数据来修改表.下面是第一种格式: UPDATE #famousjaycees SET jc = 'Jhony cash', ...
- word-ladder总结
title: word ladder总结 categories: LeetCode tags: 算法 LeetCode comments: true date: 2016-10-16 09:42:30 ...
- opencv+vs2012环境搭建教程
1. 安装OpenCV和VS. 本人电脑安装的是opencv2.4.10和vs2012 2.配置环境变量 以下以win8 64位系统为例: 计算机->属性->高级系统设置->环境变量 ...
- JS仿淘宝左侧菜单
http://www.webdm.cn/webcode/1c724a06-06f4-4c4f-931a-c683285fa700.html
- Android字体简述
Android是一个典型的Linux内核的操作系统.在Android系统中,主要有DroidSans和DroidSerif两大字体阵营,从名字就可以看出来,前者是无衬线字体,后者是衬线字体.具体来说, ...
- maven里如何根据不同的environment打包
一个项目里总会有很多配置文件.而且一般都会有多套环境.开发的.测试的.正式的.而在这些不同的环境这些配置的值都会不一样.比如mail的配置.服务的url配置这些都是很常见的.所以在打包的时候就要根据e ...