Gold mine of Visual C++ tricks!
Posts tagged A2W
How to convert Unicode to Ansi and vice versa – More Easily?
01368 days
by Jijo Raj
in Windows APIs
![]()
For converting between Unicode and Ansi and vice versa, usually we use functions such as MultiByteToWideChar() and WideCharToMultiByte(). But they are bit hard to use. Is there any other easy way to convert Unicode to Ansi and vice versa? Yes! you can use ATL conversion macros.
![]()
In ATL there are some helper macros defined such as W2A() which is used to convert widechar to ansi and A2W which is used to convert ansi to WideChar. Before using this macros you should initialize the conversion by calling USES_CONVERSION. See the code snippet below.
#include "atlconv.h" ... // Ansi string. char* pstrString = "Hello There!"; // Unicode string. wchar_t* pUnicodeString = 0; // Use this macro for Initializing the conversion. // Actually, some member variable initialization deep inside. USES_CONVERSION; // Convert Ansi to Unicode. pUnicodeString = A2W( pstrString); // Convert Unicode to Ansi. pstrString = W2A( pUnicodeString);
![]()
Targeted Audience – Beginners.