将 lptstr 转换为 char*
有人知道如何在 C++ 中将 LPTSTR
类型转换为 char *
吗?
Would anyone happen to know how to convert type LPTSTR
to char *
in C++?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
取决于它是否显示为 Unicode。 如果不是 Unicode,则 LPTSTR 是 char*,如果是,则 LPTSTR 是 w_char*。
此处进行了更好的讨论(接受的答案值得一读)
Depends if it is Unicode or not it appears. LPTSTR is char* if not Unicode, or w_char* if so.
Discussed better here (accepted answer worth reading)
这里有很多方法可以做到这一点。 MFC 或ATL 的CString、ATL 宏或Win32 API。
您可以使用 ATL 宏来转换:
CString:
或 Win32 API
WideCharToMultiByte
(如果定义了UNICODE
)。Here are a lot of ways to do this. MFC or ATL's CString, ATL macros, or Win32 API.
You can use ATL macros to convert:
CString:
or the Win32 API
WideCharToMultiByte
ifUNICODE
is defined.如果您的编译器字符设置设置为 Unicode 字符集,则 LPTSTR 将被解释为 wchar_t*。 在这种情况下,需要将 Unicode 字符转换为多字节字符。
(在 Visual Studio 中,设置位于 Project Properties\Configuration Properties\General\Character Set)
下面的示例代码应该给出一个想法:
另一方面,如果您的编译器字符设置设置为 Multibyte,则 LPTSTR 将被解释为 char*。
在这种情况下:
另请参阅:
有关 wchar_t 转换的另一个讨论:如何正确使用 WideCharToMultiByte
MSDN 文章:http://msdn.microsoft。 com/en-us/library/dd374130(v=vs.85).aspx
有效代码页标识符:http://msdn。 microsoft.com/en-us/library/dd317756(v=vs.85).aspx
If your compiler Character Setting is set to Unicode Character Set, then LPTSTR will be interpreted as wchar_t*. In that case Unicode to Multibyte character conversion is required.
(In Visual Studio, setting is located at Project Properties\Configuration Properties\General\Character Set)
The sample code below should give an idea:
On the other hand, if your compiler Character Setting is set to Multibyte, then LPTSTR will be interpreted as char*.
In that case:
Also see:
Another discussion about wchar_t conversion: How do you properly use WideCharToMultiByte
MSDN Article: http://msdn.microsoft.com/en-us/library/dd374130(v=vs.85).aspx
Valid Code Page Identifiers: http://msdn.microsoft.com/en-us/library/dd317756(v=vs.85).aspx
好的,假设您必须使用 Unicode。 你使用一些像 LookupAccountSid 这样的函数,它们是你的程序运行所必需的 - 但它们返回 LPTSTR 来获取你需要作为字符串处理的重要信息(无论出于何种原因 - 它是编程,会发生这样的事情)
现在,如果你使用多字节 - 这不是问题。 但有一个方法可以解决它。 这是我的方法,诚然是草率的。 但尽管如此,您应该能够看到它是如何工作的。
有任何问题就问吧。 我意识到这是一篇旧帖子 - 但我喜欢为将来来看的人发帖。 (像我这样的人)
OK, so lets say that you HAVE to use Unicode. And you use some functions like LookupAccountSid, that are required for your program to function - but they return LPTSTR for important information you NEED to process as a string (for whatever reason - it's programming, stuff like this happens)
Now, if you were using multibyte - this wouldn't be an issue. But there is a way to solve it. This is my method and is admittedly sloppy. But nonetheless you should be able to see how it works.
Any questions just ask. I realise this is an old post - but I like to post for people in the furture that come looking. (people like me)
我希望这对某人有帮助,因为我花了一段时间才弄清楚如何做到这一点。
首先,
LPTSTR
是指针类型,它基本上等同于TCHAR*
(假设包含
) 。请注意,
TCHAR
的大小因字符编码类型而异。 即如果定义了 unicode,则TCHAR
等于wchar_t
,否则为char
。当然,如果将宽字符转换为普通的
char
,则只能保留LSB,并且可能会丢失一些数据。 这对我来说有点恼火。 所以我写了下面的代码。 它的主要优点是进行转换时不会丢失任何数据。顺便说一下,如果您同意数据丢失,那么
wcstombs
完成这项工作。
I hope this helps someone, because it took me a while to figure out how to do it.
First of all,
LPTSTR
is of pointer type and it is basically equivalent toTCHAR*
(assuming that<tchar.h>
is included).Note that the size of
TCHAR
varies based of the character encoding type. i.e. if unicode is defined,TCHAR
is equal towchar_t
, otherwise it ischar
.Naturally, if you convert a wide character to a normal
char
, you can only keep the LSB and may lose some data. This was somehow irritating for me. so I wrote the following code. Its main advantage is doing the conversion without losing any data.By the way, if you are okay with data loss, then
wcstombs
does the job.我错过了一些简单的例子,所以这里是:(
对我来说 char* 与 char[] 相同)
I was missing some simple example so here it is:
(for me char* is identical to char[])
毫无疑问,许多人(例如,我们 unix 人)会对疯狂的 Microserf 双语感到恐惧 - “如果你的编译器处于 Unicode 模式,请使用 LPWSTR 或在其前面添加一个“T_”,但前提是它是静态的字符串,与“L”相同,或者如果使用 ATL,则使用 T2A(),但现在已经过时了;或者使用 VARIANT,但如果与 COM/OLE 链接则不使用”...)。
本页列出的“if (sizeof(TCHAR) == sizeof(char))”是对一个不错的解决方案的逻辑尝试,但它不会编译 - if-true 不会编译或 if-false 不会编译' 编译,具体取决于您的编译器标志(啊!)。
对于写后忘记的便携式解决方案,您需要求助于[过于通用的命名] UNICODE 宏。 我提供了对之前代码的修改:
no doubt many (eg, us unix folk) will recoil in horror at the mad-hatter Microserf doublespeak - "if your compiler is in Unicode mode, use LPWSTR or stick a "T_" in front of it, but only if it's a static string, which is the same as an "L", or use T2A() if using ATL, but that's now outdated, or use VARIANT but not if linking with COM/OLE"...).
The "if (sizeof(TCHAR) == sizeof(char))" listed on this page is a logical attempt at a nice solution, but it won't compile - either the if-true won't compile or if-false wont' compile, depending on your compiler flags (Aaargh!).
For a write-and-forget portable solution you'll need to resort to the [too-generic named] UNICODE macro. I offer this adaptation of the previous code: