Re: C++ type conversion question
Alex Smotritsky <[email protected]> Tue, 5 Dec 2006 01:39:58 -0700
| Newsgroups | gmane.comp.windows.devel.dotnet.cx |
|---|---|
| Message-ID | <000301c71848$f333aaf0$9a09a8c0@animal> |
Thanks to all for the help. -----Original Message----- From: Discussion relating to the specifics of the C# and Managed C++ languages [mailto:[email protected]] On Behalf Of Efran Cobisi Sent: Tuesday, December 05, 2006 1:29 AM To: [email protected] Subject: Re: [DOTNET-CX] C++ type conversion question 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/unicod e_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 DevelopMentorR http://www.develop.com > > View archives and manage your subscription(s) at http://discuss.develop.com > =================================== This list is hosted by DevelopMentorR 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