Solved

UNICODE 2 ANSI conversions...

Posted on 1999-01-22
9
264 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 200 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

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: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

746 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

12 Experts available now in Live!

Get 1:1 Help Now