如何从LPCTSTR转换为std :: string? [英] How do I convert from LPCTSTR to std::string?

查看:139
本文介绍了如何从LPCTSTR转换为std :: string?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 LPCTSTR ,并且想要调用一个带有 std :: string p>

我需要做哪些转换?

解决方案



LPCTSTR 可以是单字节或多字节字符串(取决于 UNICODE 常量在编译期间被定义),而 std :: string 的用户(包括您的函数)通常使用它来保存单个-byte string。



您需要两次转换: LPCSTR (非 - UNICODE build),另一个用于 LPCWSTR UNICODE build)。第一个很简单:

  std :: string convert(LPCSTR str){
return std :: string str);
}

第二个需要将其输入参数转换为另一个编码, a href =http://msdn.microsoft.com/en-us/library/dd374130%28v=VS.85%29.aspx> WideCharToMultiByte 。不要为名称感到惊讶,结果可以是单字节的char字符串;取决于 CodePage 参数。您必须为单字节编码使用代码页,例如 CP_ACP



更新: WideCharToMultiByte 示例



请注意,准确转换为单字节编码技术上不可能的,如果输入字符串包含不存在于目标编码的代码页中的字符。由于你提到它将是文件系统函数,如果文件路径包含这样的字符,转换将不是100%准确和随后函数调用将失败。

  std :: string MBFromW(LPCWSTR pwsz,UINT cp){
int cch = WideCharToMultiByte (cp,0,pwsz,-1,0,0,NULL,NULL);

char * psz = new char [cch];

WideCharToMultiByte(cp,0,pwsz,-1,psz,cch,NULL,NULL);

std :: string st(psz);
delete [] psz;

return st;
}

注意事项代码我躺在周围,不是生产级质量。一个显而易见的缺陷是它不是异常安全的。它也可能杀死所有漂亮的紫色独角兽。

$



完整的编码地狱



真的是 std :: string 可以用于多字节编码(例如UTF8)很好 - 你甚至可以使用它来保存宽字符串,因为它只是



问题是适用于 std :: string 的STL函数, code>期望其内容采用单字节编码,如果不是这样,则它们不会产生正确的结果。



通过扩展,我们不知道你的函数需要 std :: string 参数期望 - 它可能需要一个以UTF编码的字符串-8。但是按约定,我假设它也想要一个单字节编码的字符串。


I have an LPCTSTR and want to call a function that takes a std::string parameter.

What conversion do I need to do?

解决方案

Tip of the iceberg

LPCTSTR can be either a single-byte or a multibyte string (depends on the UNICODE constant being defined during compilation or not), while std::string's users (including your function) normally use it to hold a single-byte string.

You 'd need two conversions: one for LPCSTR (non-UNICODE build) and one for LPCWSTR (UNICODE build). The first one is simple:

std::string convert(LPCSTR str) {
    return std::string(str);
}

The second one needs its input parameter to be converted to another encoding first with WideCharToMultiByte. Do not be alarmed by the name, the result can be a single-byte char string; that depends on the CodePage parameter. You will have to use a codepage for a single-byte encoding, such as CP_ACP.

Update: WideCharToMultiByte example

Be aware that accurately converting to a single-byte encoding is technically impossible if the input string contains characters not existing in the target encoding's code page. Since you mention it's going to be for filesystem functions, if the file path contains such characters the conversion will not be 100% accurate and the subsequent function calls will fail.

std::string MBFromW(LPCWSTR pwsz, UINT cp) {
    int cch = WideCharToMultiByte(cp, 0, pwsz, -1, 0, 0, NULL, NULL);

    char* psz = new char[cch];

    WideCharToMultiByte(cp, 0, pwsz, -1, psz, cch, NULL, NULL);

    std::string st(psz);
    delete[] psz;

   return st;
}

Caveat emptor: The example above is from some code I had lying around and is not production-grade quality. The one immediately obvious flaw is that it is not exception-safe. It might also kill all the nice purple unicorns. Use it only as an example.

The full encoding hell

The naked truth is that std::string can be used for multibyte encodings (such as UTF8) just fine -- you can even use it to hold wide-char strings, since it's just a binary-safe array of bytes at heart.

The problem is that the STL functions that apply to std::string expect its contents to be in a single-byte encoding, and they won't produce correct results if this is not true.

By extension, we don't know what your function that takes an std::string parameter expects -- it might expect a string encoded in UTF-8. But "by convention", I 'm assuming it also wants a single-byte-encoded string.

这篇关于如何从LPCTSTR转换为std :: string?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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