Solved

UNICODE to ANSI

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 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.

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