tags:

views:

41

answers:

3

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.

+2  A: 

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...

Dean Harding
+1  A: 

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.

R Samuel Klatchko
+1  A: 

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

humbagumba