关于c_str()和char类型应用于函数参数的小问题!

jukyy 2003-04-15 01:43:49
需要调用的函数定义如下:
BOOL __fastcall TCommunication::WriteCommBlock(LPSTR lpBlock,DWORD nMaxLength)
在如下调用时不会出错(其中sendBuffer是AnsiString类型)
bool bCode=COMX->WriteCommBlock(sendBuffer.SubString(sendBuffer.Length()-sendCount+1,1).c_str(),1);
if(bCode) sendCount = sendCount-1;

而我想这样调用该函数:
char cc;
strtemp = sendBuffer.SubString(sendBuffer.Length() - sendCount + 1,2);
intlen = sendBuffer.Length();
cc = char(StrToInt("0x" + strtemp));
bCode=COMX->WriteCommBlock(cc,2); //编译出错,不能int转换成char
if(bCode) sendCount = sendCount-2;


当我按照下面这样定义调用时编译可以通过,但是我不知道结果是否对了?
char *cc;
strtemp = sendBuffer.SubString(sendBuffer.Length() - sendCount + 1,2);
intlen = sendBuffer.Length();
*cc = char(StrToInt("0x" + strtemp));
bCode=COMX->WriteCommBlock(cc,2); //编译出错,不能int转换成char
if(bCode) sendCount = sendCount-2;

小问题一个,请教高手!

...全文
56 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
hellobcb 2003-04-15
  • 打赏
  • 举报
回复
直接这样写行吗?
AnsiString cc;
strtemp = sendBuffer.SubString(sendBuffer.Length() - sendCount + 1,2);
intlen = sendBuffer.Length();
cc = "0x" + strtemp;
bCode=COMX->WriteCommBlock(cc.c_str(),2);
if(bCode) sendCount = sendCount-2;

qibo999 2003-04-15
  • 打赏
  • 举报
回复
char *cc = new char[2];
cc = StrLCopy(cc, sendBuffer.SubString(sendBuffer.Length()-sendCount+1,1).c_str(), 1);
cc[1] = '\0';
bool bCode=COMX->WriteCommBlock(cc,2);
delete cc;

试试这样行吗?

13,826

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧