2012-08-30 16 views
8

Ho ottenuto da una casella di riepilogo contenente righe di quattro parole. Quando clicco su una riga, queste parole dovrebbero essere viste in quattro diverse caselle di testo. Finora, ho tutto funzionante, ma ho un problema con la conversione dei caratteri. La stringa dalla listbox è una UnicodeString ma lo strtok utilizza un carattere []. Il compilatore indica che non è possibile convertire UnicodeString in Char []. Questo è il codice che sto usando per questo:Conversione di Unicodestring in Char []

{ 
int a; 
UnicodeString b; 

char * pch; 
int c; 

a=DatabaseList->ItemIndex; //databaselist is the listbox 
b=DatabaseList->Items->Strings[a]; 

char str[] = b; //This is the part that fails, telling its unicode and not char[]. 
pch = strtok (str," ");  
c=1;       
while (pch!=NULL) 
    { 
     if (c==1) 
     { 
      ServerAddress->Text=pch; 
     } else if (c==2) 
     { 
      DatabaseName->Text=pch; 
     } else if (c==3) 
     { 
      Username->Text=pch; 
     } else if (c==4) 
     { 
      Password->Text=pch; 
     } 
     pch = strtok (NULL, " "); 
     c=c+1; 
    } 
} 

So che il mio codice non sembra bello, piuttosto male in realtà. Sto solo imparando qualche programmazione in C++. Qualcuno potrebbe dirmi come convertire questo?

risposta

8

strtok modifica effettivamente il proprio array di caratteri, quindi è necessario creare una serie di caratteri che è possibile modificare. Il riferimento direttamente alla stringa UnicodeString non funzionerà.

// first convert to AnsiString instead of Unicode. 
AnsiString ansiB(b); 

// allocate enough memory for your char array (and the null terminator) 
char* str = new char[ansiB.Length()+1]; 

// copy the contents of the AnsiString into your char array 
strcpy(str, ansiB.c_str()); 

// the rest of your code goes here 

// remember to delete your char array when done 
delete[] str; 
+0

Grazie mille !! Sto andando a scoprire come ha funzionato quello che hai fatto, ma ha funzionato! –

+0

Ho recentemente modificato il mio post per rendere il char array di lunghezza ansiB.Length() + 1. Questo è importante. Assicurati di apportare questa modifica o potresti ottenere arresti anomali casuali. –

+0

Ricevo un errore dopo aver chiuso il programma. Quando ho usato il codice che hai fornito, ricevo una violazione di accesso, quando lo interrompo salta su Forms.hpp /* TCustomForm.Destroy */inline __fastcall virtual ~ TForm (void) {} ​​ Potrebbe significare che c'è qualcosa che sta andando sbagliato con lo str o qualcosa del genere? –

0

Questo funziona per me e mi fa risparmiare la conversione in AndiString

// Using a static buffer 
#define MAX_SIZE 256 
UnicodeString ustring = "Convert me"; 
char mbstring[MAX_SIZE]; 

    wcstombs(mbstring,ustring.c_str(),MAX_SIZE); 

// Using dynamic buffer 
char *dmbstring; 

    dmbstring = new char[ustring.Length() + 1]; 
    wcstombs(dmbstring,ustring.c_str(),ustring.Length() + 1); 
    // use dmbstring 
    delete dmbstring; 
Problemi correlati