K
K
Kvert0072014-02-27 11:25:01
LDAP
Kvert007, 2014-02-27 11:25:01

How to implement converting unicode string to utf-8 C++?

Hello! There is a project on VS 2005, going to unicode.
Here's a snippet of code:

string LDAP="LDAP://";
  string path = LDAP + strDesc;
  wstring wstr( path.begin(), path.end() );
  LPCTSTR path2 = W2CT( wstr.c_str( ) );
  hr = ADsGetObject( path2, IID_IADs,(void**)&pUsr);

Everything works fine if there are no Russian characters in strDesc. In ADsGetObject, the request path is passed as the first parameter, this is a string of type LPCTSTR , the question is whether it can be converted from Unicode to UTF-8, since it is written in LDAP rfc that utf-8 is supported.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
N
nekipelov, 2014-02-27
@nekipelov

You can take utfcpp , just use:

std::wstring ws;
            utf8::utf8to16( str.begin(), str.end(), std::back_inserter(ws) );

            std::string str;
            utf8::utf16to8( ws.begin(), ws.end(), std::back_inserter(str) );

Or just icu , where there is everything for working with multibyte encodings.
Well, or WinAPI tools: MultiByteToWideChar

K
Kvert007, 2014-02-27
@Kvert007

In general, I decided to look at the character codes in the line, brought them to the file, I got this:

76
68
65
80
58
47
47
67
78
61
-34
-16
-24
-23
32
-49
-27
-14
-16
-18
-30
44
67
78
61
85
115
101
114
115
44
68
67
61
118
105
112
44
68
67
61
99
98
114
0

I checked the characters, they correspond to the cp1251 encoding =>
I need to get utf-8 from 1251, can you tell me more about MultiByteToWideChar?
The problem is that the string that needs to be passed as the 3rd parameter to MultiByteToWideChar is of type LPCTSTR and LPCSTR is required, how can I convert it to this type and will there be any losses?

K
Kvert007, 2014-02-27
@Kvert007

string strDesc = static_cast<CHAR*>(CW2A(bstr));//
  string LDAP="LDAP://";		//
  string path = LDAP + strDesc;
  wstring wstr( path.begin(), path.end() );	//
  LPCTSTR path2 = W2CT( wstr.c_str( ) );
  LPSTR path3 = const_cast<char *>(path.c_str());
  LPWSTR result;
  MultiByteToWideChar(CP_UTF8,0,path3,strlen(path3)+1,result, strlen(path3)+1);

why is nothing coming back

K
Kvert007, 2014-02-27
@Kvert007

If instead of hr = ADsGetObject( path2, IID_IADs,(void**)&pUsr); I write
hr = ADsGetObject( L"Russian text", IID_IADs,(void**)&pUsr); it also works, I can’t understand the reason

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question