Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

UNICODE to ANSI

Posted on 1998-06-29
2
Medium Priority
?
1,676 Views
Last Modified: 2008-03-17
I have a win32 app in which all strings and chars are defined as UNICODE.  Within the app, there exist a number of winsock calls.  When I pass a UNICODE string to a winsock function, the winsock function fails.  I thought I read somewhere that NT will automatically convert a string to ANSII if that is required by the function.  Apparently it is not working in this case.  Can someone tell me or show me of an easy way to convert from UNICODE to ANSII?
Thanks.
0
Comment
Question by:dleifocs
2 Comments
 
LVL 32

Accepted Solution

by:
jhance earned 400 total points
ID: 1166805
There is no "automatic" UNICODE to ANSI.  You must check the function call's requirements (sometimes VERY carefully as documentation is inconsistent) and pass either a UNICODE or ANSI string based on what it is expecting.  There are a few options:

1) The MFC header file AFXPRIV.H includes a definition of a macro called W2A that will take a LPCWSTR and return a LPSTR.  

2) You can call the wcstombs to convert from UNICODE to ASNI from the C runtime library.

3) You can write your own.  Just pick out every other byte until you get a NULL and return that value.
0
 

Author Comment

by:dleifocs
ID: 1166806
To jhance,

Thank you very much!!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

877 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