Re: C++ type conversion question

Efran Cobisi <[email protected]> Tue, 5 Dec 2006 09:28:34 +0100
Newsgroups gmane.comp.windows.devel.dotnet.cx
Organization QBGROUP SpA
Message-ID <[email protected]>
Alex,

Since a TCHAR* may already point to a WCHAR*, dependening on the Unicode
support for your project, it is necessary to split the code execution.
If the project is compiled with Unicode support (UNICODE is defined),
then the WCHAR* could be safely assigned to the TCHAR*. Else a
conversion is required to convert the single byte string to the Unicode
one: MultiByteToWideChar (1) should perform what you are looking for.

--------8<-------

    TCHAR* sTest = _T("Hello");
    WCHAR* wsOutput;

#ifdef  UNICODE
    // sTest is already a wide char string

    wsOutput = sTest;
#else
    // Obtain the length the output buffer will be.
    // Note I'm using the current code page to perform the conversion.

    int iOutputLen = MultiByteToWideChar(CP_ACP,
        MB_PRECOMPOSED,
        sTest,
        -1,
        NULL,
        0);

    wsOutput = new WCHAR[iOutputLen];

    MultiByteToWideChar(CP_ACP,
        MB_PRECOMPOSED,
        sTest,
        -1,
        wsOutput,
        iOutputLen);
#endif

--------8<-------

1)
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/intl/unicode_17si.asp

HTH,

Efran Cobisi
http://www.cobisi.com

Alex Smotritsky wrote:
> I have a TCHAR* referencing a string and I want to get that data into a
> WCHAR []
>
>
>
> How do I do that?
>
>
>
>
>
>
> ===================================
> This list is hosted by DevelopMentor®  http://www.develop.com
>
> View archives and manage your subscription(s) at http://discuss.develop.com
>

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com