Embarcadero无法使用' UnicodeString'到#' unsigned char *'

时间:2013-04-26 12:39:33

标签: c++ string casting c++builder unicode-string

我正在使用Embarcadero的C ++构建器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无法从'UnicodeString'转换为'unsigned char *'

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

TcpClient1->SendBuf((BYTE*)"FREQ 330.5 MHz\n",15,0);Sleep(30);
一切都好。非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

我只想使用sprintf

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

答案 1 :(得分:0)

String映射到XE中的UnicodeString。修复代码的最简单方法是使用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);