?
Solved

UNICODE 2 ANSI conversions...

Posted on 1999-01-22
9
Medium Priority
?
271 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
[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
  • 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
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

719 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