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

x
?
Solved

UNICODE 2 ANSI conversions...

Posted on 1999-01-22
9
Medium Priority
?
272 Views
Last Modified: 2013-11-20
How can I get the value from an UNICODE string converted to a char * or CString?
0
Comment
Question by:danoneb
  • 5
  • 2
  • 2
9 Comments
 

Author Comment

by:danoneb
ID: 1328193
i would appreciate some code sample.... :)
0
 
LVL 86

Accepted Solution

by:
jkr earned 800 total points
ID: 1328194
This function does what you need:

char* ANSIStringFromUNICODE( LPOLESTR pszOLE)
{
HRESULT hResult;

static char acBuffer[ BUFFER_MAX];


    hResult =   ::WideCharToMultiByte   (   CP_ACP,
                                            0,
                                            pszOLE,
                                            -1,
                                            acBuffer,
                                            sizeof  (   acBuffer),
                                            NULL,
                                            NULL
                                        );


    if  (   FAILED  (   hResult))
            return  (   NULL)

    return ( acBuffer);
}


Feel free to ask if you have further questions on this issue ;-)
0
 
LVL 86

Expert Comment

by:jkr
ID: 1328195
Ooops, i forgot: To assign it to a CString, you could simply use 'CString::operator=' (i.e. assign it directly) or modify the function slightly:

CString CStringFromUNICODE( LPOLESTR pszOLE)
{
HRESULT hResult;
CString strResult;
char acBuffer[ BUFFER_MAX];


    hResult =   ::WideCharToMultiByte   (   CP_ACP,
                                            0,
                                            pszOLE,
                                            -1,
                                            acBuffer,
                                            sizeof  (   acBuffer),
                                            NULL,
                                            NULL
                                        );


    if  (   FAILED  (   hResult))
            return  (   strResult)

    strResult = acBuffer;

    return ( strResult);
}

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 23

Expert Comment

by:chensu
ID: 1328196
You may also use the W2A macro implemented in ATL, which actually calls WideCharToMultiByte for you.

USES_CONVERSION;

LPWSTR lpwStr = L"ABC";
char *pszConverted = W2A(lpwStr);
0
 
LVL 86

Expert Comment

by:jkr
ID: 1328197
chensu - you're of course right, but you'll agree that the above code is merely a 'rollout' of that what W2A accomplishes...
0
 

Author Comment

by:danoneb
ID: 1328198
Sorry, i forgot to specify:
I need to get the content from a CComBSTR object...
0
 
LVL 86

Expert Comment

by:jkr
ID: 1328199
This makes no difference - a BSTR is mainly typedef'd as

typedef OLECHAR *BSTR;
(see wtypes.h, i just left out the RPC stuff)

So, you could use either CComBSTR::m_str directly or detach the BSTR using CComBSTR::Detach().
OR: simply call the functions above using the ( BSTR) typecast on your CCOmBSTR object...
0
 
LVL 86

Expert Comment

by:jkr
ID: 1328200
To make it clearer (still from wintypes.h):

typedef OLECHAR *LPOLESTR;
typedef OLECHAR *BSTR;

So you see, a BSTR is the same as LPOLESTR...

0
 
LVL 23

Expert Comment

by:chensu
ID: 1328201
jkr,

>but you'll agree that the above code is merely a 'rollout' of that what W2A accomplishes...

Yes, agreed. I mentioned "which actually calls WideCharToMultiByte for you.".

danoneb,

>I need to get the content from a CComBSTR object...

In this case, you can make use of _bstr_t class that has an overloaded operator char*().

#include <comdef.h>

CComBSTR bstrData1;

_bstr_t bstrData2(bstrData1, false);

// Then use bstrData2 in place of char *

You need to use try-catch to capture the possible exceptions because both the constructor and the operator might throw _com_error.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Suggested Courses

885 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