CW2A(LPCWSTR)str)和CW2A(LPCWSTR)str,CP_UTF8)有什么区别? [英] What is difference between CW2A(LPCWSTR)str) and CW2A(LPCWSTR)str, CP_UTF8)?

查看:265
本文介绍了CW2A(LPCWSTR)str)和CW2A(LPCWSTR)str,CP_UTF8)有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将几个 CStringW 字符串转换为 CStringA 字符串.字符串之一(我们称其为otherLangString)是其他语言(中文,阿拉伯语等).像这样使用时,所有其他字符串都没有转换的问题:

I am trying to convert few CStringW strings to CStringA strings. One of the strings (lets call it otherLangString) is in other language (Chinese, Arabic etc.). All the other strings had no issue getting converted when used like this :

CW2A((LPCWSTR)some_String);

但是当用于otherLangString时,我得到"?????"所以要解决这个问题,我做到了,而且奏效了

But when used for the otherLangString, I was getting "?????" So to fix that, I did this and it worked

CW2A(some_String, CP_UTF8);

现在代码中的所有转换看起来像第一个样本,除了一个看起来像第二个样本.

Now in the code some all conversions looked like the 1st sample except one which looked like the 2nd sample.

为了保持一致,我将二者混合在一起,并全部做到了.

For consistency I mixed above two and did this for all.

CW2A((LPCWSTR)some_String, CP_UTF8);

我的问题是,关注之间有什么区别?

My questions is, What is the difference between following ?

- CW2A((LPCWSTR)some_String, CP_UTF8) and CW2A(some_String, CP_UTF8);
- CW2A((LPCWSTR)some_String) and CW2A(some_String, CP_UTF8);

推荐答案

CW2A

CW2A is a typedef for CW2AEX<>, and it's c'tor is documented. The c'tor taking 2 arguments allows you to explicitly specify the code page to use for the conversion:

nCodePage:
用于执行转换的代码页.有关更多详细信息,请参见Windows SDK函数
MultiByteToWideChar 的代码页参数讨论.

如果不指定代码页,则使用当前线程的ANSI代码页进行转换(您很少希望这样做).这在 ATL和MFC字符串转换宏中进行了解释:

If you don't specify a code page, the current thread's ANSI code page is used for the conversion (you rarely want that). This is explained under ATL and MFC String Conversion Macros:

默认情况下,ATL转换类和宏将使用当前线程的ANSI代码页进行转换.如果要使用基于类 CA2WEX CW2AEX 的宏对特定转换覆盖该行为,请指定代码页作为该类的构造函数的第二个参数.

By default, the ATL conversion classes and macros will use the current thread's ANSI code page for the conversion. If you want to override that behavior for a specific conversion using macros based on the classes CA2WEX or CW2AEX, specify the code page as the second parameter to the constructor for the class.

就您而言,

CW2A((LPCWSTR)some_String);

使用线程的当前ANSI代码页从UTF-16转换为窄字符串.仅当使用相同的ANSI代码页进行解释时,结果才有意义.更糟糕的是,ANSI代码页编码的字符串不能表示所有Unicode字符.

converts from UTF-16 to a narrow character string, using the thread's current ANSI code page. The result is only meaningful when interpreted using the same ANSI code page. To make matters worse, ANSI code page encoded strings cannot represent all Unicode characters.

另一段代码

CW2A(some_String, CP_UTF8);

从UTF-16转换为UTF-8.这通常是有利的,因为转换是无损且明确的.两种编码都可以表示相同的字符集.任何能够解释UTF-8的阅读器都可以对编码后的字符串进行解码.

converts from UTF-16 to UTF-8. This is generally favorable, since the conversion is lossless and explicit. Both encodings can represent the same set of characters. The encoded string can be decoded by any reader capable of interpreting UTF-8.


注意:通常,您不能直接使用Windows中存储在 CStringA 中的UTF-8编码的字符串.通过网络发送内容或将内容写入磁盘是安全的.但是,如果要将其传递给Windows API(例如用于显示),则必须先转换为UTF-16.Windows API的ANSI版本不支持UTF-8.


Note: In general, you cannot directly use a UTF-8 encoded character string stored in a CStringA in Windows. It is safe to send the contents over a network, or write them to disk. But if you want to pass it to the Windows API (e.g. for display) you have to convert to UTF-16 first. The ANSI versions of the Windows API do not support UTF-8.

这篇关于CW2A(LPCWSTR)str)和CW2A(LPCWSTR)str,CP_UTF8)有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆