?
Solved

CHAR * to WCHAR

Posted on 1999-11-19
2
Medium Priority
?
973 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:jas123
2 Comments
 
LVL 1

Accepted Solution

by:
allym earned 100 total points
ID: 2219096
sizeof (body) is sizeof (void*).  You really want to pass the length of the string in, don't you?

Use char* to define the string, and pas strlen(body) instead.  

Its one obvious thing that's wrong anyway. . .
0
 

Expert Comment

by:cloder
ID: 2220286
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).
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question