[unicode] Convertir CString en Char[]

cs_nitrique Messages postés 344 Date d'inscription jeudi 1 mai 2003 Statut Membre Dernière intervention 4 avril 2011 - 30 mai 2006 à 16:01
excrt Messages postés 75 Date d'inscription mercredi 5 avril 2006 Statut Membre Dernière intervention 3 juillet 2006 - 30 mai 2006 à 18:19
Bonjour,

Selon toutes les sources que j'ai pu voir, ça devrait être correct:
<!-- BEGIN TEMPLATE: bbcode_code -->

char  *ptr,temp[MAX_LOADSTRING];
CString strValue;
ptr = temp;
... CODE ...
if (strValue.GetLength()>0) {
SendMessage(hEdtT,EM_SETSEL,TextLen,TextLen); // Placer le curseur à la fin
SendMessage(hEdtT,EM_REPLACESEL,0,(LPARAM)(LPCTSTR)strValue); // Ecrit le texte
TextLen += strValue.GetLength();

/*WideCharToMultiByte(CP_ACP,0,strValue,-1,temp,wcslen(strValue)+1,NULL,NULL); // CString->char
strValue.Format(L">%s<",temp);
SendMessage(hEdtT,EM_SETSEL,TextLen,TextLen); // Placer le curseur à la fin
SendMessage(hEdtT,EM_REPLACESEL,0,(LPARAM)(LPCTSTR)strValue); // Ecrit le texte
TextLen += strValue.GetLength(); // */
}

Le premier affichage marche bien tout seul, mais dès que je met le reste du code (ce qui est commenté) ça pête (pleins de caractères bizzares) !

Pourquoi ça ne marche pas ?

David, à VERSAILLES
http://www.gentag.fr
A voir également:

3 réponses

excrt Messages postés 75 Date d'inscription mercredi 5 avril 2006 Statut Membre Dernière intervention 3 juillet 2006
30 mai 2006 à 16:23
ton « strValue.Format(L">%s<",temp); » s'attend a recevoir un « wchar_t* » et tu lui envois un « char* »
tu devrais plutôt convertir ton « char temp[...] » en wchar_t

char  *ptr, temp[MAX_LOADSTRING];
wchar_t wtemp[MAX_LOADSTRING]; //- wide version
CString strValue;

ptr = temp;
//- ... CODE ...

if (strValue.GetLength() > 0)
{
  SendMessage(hEdtT, EM_SETSEL, TextLen, TextLen);
  SendMessage(hEdtT, EM_REPLACESEL, 0, (LPARAM)(LPCTSTR)strValue);
  TextLen += strValue.GetLength();

  //- char to wchar_t
  MultiByteToWideChar(CP_ACP, 0, temp, -1, wtemp, strValue.GetLength() + 1, NULL, NULL);
  strValue.Format(L">%s<", wtemp);

  SendMessage(hEdtT, EM_SETSEL, TextLen, TextLen);
  SendMessage(hEdtT, EM_REPLACESEL, 0, (LPARAM)(LPCTSTR)strValue);
  TextLen += strValue.GetLength();
}

-=-= ExCRT =-=-
0
cs_nitrique Messages postés 344 Date d'inscription jeudi 1 mai 2003 Statut Membre Dernière intervention 4 avril 2011 1
30 mai 2006 à 16:27
Bonjour excrt,

Le but est justement de le caster en tableau de char pour le passer ensuite à une fonction qui attend ce type en parametre.

[;)] David, à VERSAILLES
http://www.gentag.fr
0
excrt Messages postés 75 Date d'inscription mercredi 5 avril 2006 Statut Membre Dernière intervention 3 juillet 2006
30 mai 2006 à 18:19
oooohhhhh!!!! plus de précision stp ...

ton CString == « wchar_t » et tu veux lui faire formater du « char », converti ton « char » en « wchar_t » et lance ton .Format() _ensuite_

c'est normal d'avoir des caractères bizarre, un « wchar_t » == deux « char »

sizeof(char) >> 1
sizeof(wchar_t) >> 2

//- abc
char ac[] = {0x41, 0x42, 0x43, 0x00}; //- trois caractères plus le nul( 0x00 == \0 )wchar_t* wc (wchar_t*)ac; //- {0x4142, 0x4300} DEUX caractère sans le nul( \0 )
//- wcout ==>> wide cout
wcout << wc << endl;
//- plutôt que d'afficher « abc », il va afficher « DES CARACTÈRES BIZARRES »

donc: conversion, conversion, conversion, conversion, ...
pour le reste, soit plus claire!

MultiByteToWideChar()/WideCharToMultiByte() sont simple a utiliser, je vois pas
ou est le problème ...

en passant, moi j'ai répondu a cette question:

//- Le premier affichage marche bien tout seul, mais dès que je met le
reste du code (ce qui est commenté) ça pête (pleins de caractères
bizzares) !
//- Pourquoi ça ne marche pas ?

-=-= ExCRT =-=-
0
Rejoignez-nous