FRC
FRC

Reputation: 485

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.

Upvotes: 0

Views: 2925

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 597951

String maps to UnicodeString in XE. The simplest way to fix your code is to use AnsiString instead (and stop using the += operator since you are not using it correctly):

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

AnsiString full = command1 + IntToStr((index+2)*10) + command2;
TcpClient1->SendBuf((BYTE*)full.c_str(),full.Length(),0);
Sleep(30);

Alternatively:

int index = Form1->ListBox1->ItemIndex;
AnsiString full;
full.sprintf("FREQ %d Mhz\n", (index+2)*10);
TcpClient1->SendBuf((BYTE*)full.c_str(),full.Length(),0);
Sleep(30);

Upvotes: 1

john
john

Reputation: 8027

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);

Upvotes: 0

Related Questions