Solved

UNICODE to ANSI

Posted on 1998-06-29
2
1,666 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 100 total points
Comment Utility
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
Comment Utility
To jhance,

Thank you very much!!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now