Make your own free website on Tripod.com
 
« May 2006 »
S M T W T F S
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
You are not logged in. Log in

Programming Tips
Saturday, 20 May 2006
How to convert a ANSI string to UNICODE string and vice versa?
Mood:  don't ask
Well quite simple,  but still quite frequently asked in forums... :)

There are two macros that does this for us. They are as follows.

Note: You must include atlconv.h

A2W - ANSI to UNICODE
W2A - UNICODE to ANSI

Before using these two macros you have to use this macro too...

USES_CONVERSION

Here is a code snippet for you... ;)

//#include <atlconv.h>
//An example for converting from ANSI to UNICODE

//use this first
USES_CONVERSION;

//An ANSI string
LPSTR lpsz_ANSI_String = "An ANSI String";

//ANSI string being converted to a UNICODE string
LPWSTR lpUnicodeStr = A2W( lpsz_ANSI_String )


//Another example for converting from UNICODE to ANSI

//Use this first
USES_CONVERSION

//A UNICODE string
LPWSTR lp_UNICODE_STR = L"A Unicode String";

//UNICODE string being converted to a ANSI string
LPSTR  lpsz_ANSI_STR = W2A( lp_UNICODE_STR );

So that's it we are done with it.

Ah wait don't leave here's a homework for you...

Rip apart CW2A and CA2W  :))

Posted by Nibu babu thomas at 3:49 PM
Updated: Saturday, 20 May 2006 3:53 PM

View Latest Entries