Search code examples
c++stringcastingc++builderunicode-string

Embarcadero Cannot cast from 'UnicodeString' to 'unsigned char *'


I'm using Embarcadero's C++ builder XE

String command1 = "FREQ ";
String command2 = " Mhz\n";
int index = Form1->ListBox1->ItemIndex;

String full = command1.operator +=(IntToStr((index+2)*10)).operator +=(command2);
TcpClient1->SendBuf((BYTE*)full,13,0);Sleep(30);

and on the last line I get E2031 Cannot cast from 'UnicodeString' to 'unsigned char*'

What I'm sending is a command for my device (receiver) which format is (command, lenght_of_command). When I send it in plan text like

TcpClient1->SendBuf((BYTE*)"FREQ 330.5 MHz\n",15,0);Sleep(30);

everything is ok. Much thanks for any help.


Solution

  • I would just use sprintf

    char command[999];
    sprintf(command, "FREQ %d MHz\n", (index+2)*10);
    TcpClient1->SendBuf((BYTE*)command,strlen(command),0);
    Sleep(30);