Solved

UNICODE to ANSI

Posted on 1998-06-29
2
1,668 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
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Fast, free, private online code repository / editor 7 97
Would like to move button in a function 3 77
Safe conversion? 4 67
gdb doesn't stop on breakpoint 2 68
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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…
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.

777 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