Link to home
Start Free TrialLog in
Avatar of jas123
jas123

asked on

CHAR * to WCHAR

I have the following piece of code:

void *body = "testcode";

wchar_t  *d_Body = SysAllocStringLen(NULL,255);

MultiByteToWideChar(CP_ACP,NULL,body,sizeof(body),d_Body,sizeof(d_Body));


But when I convert d_Body again to char * and print its value to file,the value printed is junk.


Can I solve the above problem?

Regards
Jasmina
ASKER CERTIFIED SOLUTION
Avatar of allym
allym

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cloder
cloder

Make sure you are printing/writing the text with the correct functions.

You would use printf() for ASCII chars, but wprintf for wide chars.

A better way to do your code is to avoid the separate function call altogether and define your code using the standard Windows macro _T.

WCHAR* body = _T("testcode");

If you compile your program with Unicode enabled (define _UNICODE), this will make sure that body is a wide string already.

Also, if you do not object to using MFC, Microsoft has a very convenient macro called A2W() which converts an ASCII string (not just literal stringS) to wide chars. It's useful as long as you don't need to use the wide string outside of the current scope (e.g., outside the current function).

Finally, though this may not apply to simple strings, you can't always convert a wide string to an ASCII string (because most wide characters can't be represented in ASCII, e.g. Chinese characters).