Solved

UNICODE 2 ANSI conversions...

Posted on 1999-01-22
9
266 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

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: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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.

773 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