8

4 つの単語の行を含むリストボックスを含むフォームがあります。1 行をクリックすると、これらの単語が 4 つの異なるテキスト ボックスに表示されます。これまでのところ、すべてが機能していますが、文字変換に問題があります。

リストボックスからの文字列は UnicodeString ですが、strtok は char[] を使用します。コンパイラは、「UnicodeString を 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;
    }
}

私のコードが見栄えがよくないことはわかっていますが、実際にはかなり悪いです。私はC++でプログラミングを学んでいます。

どうすればこれを変換できますか?

4

2 に答える 2

8

strtok は実際に char 配列を変更するため、変更できる文字の配列を作成する必要があります。UnicodeString 文字列を直接参照しても機能しません。

// 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;  
于 2012-08-30T15:06:30.710 に答える
0

これは私にとってはうまくいき、AnsiStringへの変換を節約します

// 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;
于 2014-08-10T19:55:23.587 に答える