Converte AnsiString para char[]


O exemplo a seguir leva um Button, um Label e 4 Edits no Form. Quando o usuário dá um clique no botão, o programa realiza algumas conversões entre AnsiString e char[] e vice-versa:


//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)
{
Edit1 -> Text = "String inicial de Edit1";        
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
char buffer[100] = "Valor inicial de buffer";
Label1 -> Caption = buffer;
Edit2 -> Text = buffer[0];
strcpy (buffer, Edit1->Text.c_str());  // converte Text de AnsiString para char*
Edit3 -> Text = buffer[0];
Edit4 -> Text = (String) buffer[0]+ buffer[1] + buffer[2];
}
//---------------------------------------------------------------------------

HOME || MAPA DO SITE || CURSOS || TUTORIAIS || LINKS || FORUM || CONTATO