Solved

UNICODE 2 ANSI conversions...

Posted on 1999-01-22
9
265 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Rewrite URL With Encoded Characters 6 75
Process filename extension 3 172
Line meaning 9 82
mapAB Challlenge 35 128
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

867 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

18 Experts available now in Live!

Get 1:1 Help Now