开发者

char array to LPCTSTR

开发者 https://www.devze.com 2023-01-02 22:05 出处:网络
May I know how I can perform the following conversion? // el.strCap is char[50] // InsertItem is expecting TCHAR pointer (LPCTSTR)

May I know how I can perform the following conversion?

// el.strCap is char[50]
// InsertItem is expecting TCHAR pointer (LPCTSTR)
// How I can perform conversion?
// I do 开发者_开发技巧not have access in both "list" and "el" source code
// Hence, there is no way for me to modify their signature.
list.InsertItem(i, el.strCap);

And No. I do not want to use

WideCharToMultiByte

They are too cumbersome to be used.


If you're using ATL, then you can use the various macros and helper classes that it includes to do the conversion:

char *test = "Hello World";
CA2CT ct(test);
list.InsertItem(i, ct);

Though saying WideCharToMultiByte is too cumbersome is a bit disingenious, in my opinion. It's easy enough to wrap a call to WideCharToMultiByte and make it return an std::wstring or whatever you need. In fact, that's basically what CA2CT is doing under the covers...


If your character string is encoded as ISO-8859-1, it's easy to convert to UTF-16:

// Convert an ISO-8859-1 string to a UTF-16 string
wchar_t wstr[50];
for (int i = 0; i < 50; ++i)
{
    wstr[i] = el.strCap[i];
    if (!wstr[i])
        break;
}

But if your data is anything other then ISO-8859-1 (or ASCII which is a subset), then you will need to handle a more complex conversion. Once you need to do that, you will find that MultiByteToWideChar is not that cumbersome in comparison.


You could also use CStringW i.e. list.InsertItem(i, CStringW("blah"));

0

精彩评论

暂无评论...
验证码 换一张
取 消