Converting Unicodestring to Char []

I have a Listbox that contains strings of four words. When I click on one line, these words should be visible in four different text blocks. So far, everything works for me, but I have a problem with character conversion. The string from the list is UnicodeString, but strtok uses char []. The compiler says met Unable to convert UnicodeString to char []. This is the code I use for this:

{ 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; } } 

I know that my code does not look beautiful, actually not bad. I am just learning some C ++ programs. Can someone tell me how to do this?

+8
c ++ builder chars
source share
2 answers

strtok actually modifies your char array, so you will need to create an array of characters that you can change. A link directly to a UnicodeString string will not work.

 // 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; 
+8
source share

This works for me and saves me 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; 
0
source share

All Articles