Bonjour � vous !
Je cherche � convertir un _bstr_t en string pour pouvoir exploiter la chaine de caract�re dans d'autres parties de mon projet. Pouvez-vous m'�clairer sur le proc�d� de conversion?
Je vous remercie d'avance !
Version imprimable
Bonjour � vous !
Je cherche � convertir un _bstr_t en string pour pouvoir exploiter la chaine de caract�re dans d'autres parties de mon projet. Pouvez-vous m'�clairer sur le proc�d� de conversion?
Je vous remercie d'avance !
En principe c'est aussi simple que ceci:
Ou alors tu peux chipoter avec WideCharToMultiByte.Code:
1
2
3
4 _bstr_t bs; //remplir bs... char const *pstr = bs; std::string s(pstr);
Ou encore tu adaptes ce code � tes besoins (conversion d'un wstring en string)
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 std::string wcs2str(std::wstring const & ws) { //a try for UTF16 conversion to ANSI (CP1252) std::string s(ws.size(), ' '); std::string::iterator it = s.begin(); for (wchar_t wc : ws) { switch (wc) { case 8364: *it = ''; break; //case 129: '' case 8218: *it = ''; break; case 402: *it = ''; break; case 8222: *it = ''; break; case 8230: *it = ' '; break; case 8224: *it = ''; break; case 8225: *it = ''; break; case 710: *it = ''; break; case 8240: *it = ''; break; case 352: *it = ''; break; case 8249: *it = ''; break; case 338: *it = ''; break; //case 141: '' case 381: *it = ''; break; //case 143: '' //case 144: '' case 8216: *it = ''; break; case 8217: *it = ''; break; case 8220: *it = ''; break; case 8221: *it = ''; break; case 8226: *it = ''; break; case 8211: *it = ''; break; case 8212: *it = ''; break; case 732: *it = ''; break; case 8482: *it = ''; break; case 353: *it = ''; break; case 8250: *it = ''; break; case 339: *it = ''; break; //case 157: '' case 382: *it = ''; break; case 376: *it = ''; break; default: if (wc < 256) *it = (unsigned char)wc; else *it = '?';//TODO check U+D800 to U+DFFF surrogate pairs } ++it; } return s; }
Et le summum du summum (8O:weird:) est d'utiliser les fonctionnalit�s de <codecvt> apr�s avoir converti le_bstr_t en std::wstring.
Mais l�, il faut potasser la doc du standard car je connais pas trop... :?;)Code:
1
2
3
4
5
6 #include <codecvt> std::wstring ws = ...; std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> cvt; std::string s = cvt.to_bytes(ws);
Le premier code post� par camboui est cens� marcher.
Le second est... int�ressant (et n'a pas l'air incorrect).
Je vois que la question n'a pas encore �t� marqu�e comme r�solue...
J'ajoute donc un lien vers une question semblable � laquelle bacelar et M�dinoc ont avantageusement r�pondu :D
convertion-lpwstr-lpcstr/
Et o� je d�couvre les macros de conversion propos�es par ATL :applo:
Dans un sujet identique
http://www.developpez.net/forums/d16...s/#post8745660
Code:
1
2 std::wstring to_string(const std::string& s) { std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter; return converter.from_bytes(s); } std::string to_string(const std::wstring& s) { std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter; return converter.to_bytes(s); }