0

我正在使用 Embarcadero 的 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);

在最后一行我得到 E2031 Cannot cast from 'UnicodeString' to 'unsigned char*'

我发送的是我的设备(接收器)的命令,格式为(命令,lenght_of_command)。当我以计划文本发送它时

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

一切都好。非常感谢您的帮助。

4

2 回答 2

1

String映射到UnicodeStringXE。修复代码的最简单方法是AnsiString改用(并停止使用+=运算符,因为您没有正确使用它):

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

或者:

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);
于 2013-05-03T23:10:01.783 回答
0

我只会使用 sprintf

char command[999];
sprintf(command, "FREQ %d MHz\n", (index+2)*10);
TcpClient1->SendBuf((BYTE*)command,strlen(command),0);
Sleep(30);
于 2013-04-26T12:45:37.343 回答