Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

UNICODE to ANSI

Posted on 1998-06-29
2
Medium Priority
?
1,675 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

670 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