增强Serialiaze档案转换为char
boost serialiaze archive convert to char
我已经成功地序列化了一个用以下代码的对象:
void aTodo::SendBack(protocolBaseServer & in){
std::ostringstream archive_stream;
boost::archive::text_oarchive archive(archive_stream);
archive << *this;
std::string outbound_data=archive_stream.str();
//type needs to be first for factory to create correct object
outbound_data=_initType+outbound_data;
//now just send it along us
in.DoWrite(outbound_data);
}
这是protocolbaseser dowrite:
void protocolBaseServer::DoWrite(std::string inMessage){
_totalMessage=inMessage;
std::cout << "totalMessage: " << _totalMessage << std::endl;
int32_t dataLength=_totalMessage.length();
int32_t orgdataLength=_totalMessage.length();
std::cout << "data length: " << orgdataLength << std::endl;
//read in todo
dataLength = htonl(dataLength ); // Ensure host system byte order on the int;from the network will be network byte order
std::cout << "writing length now" << std::endl;
protoService<int32_t>::DoWrite(_sock,&dataLength,sizeof(int32_t));
std::cout << "written length" << std::endl;
std::cout << "writing :" << _totalMessage.data() << std::endl;
protoService<int32_t>::DoWrite(_sock,(void*)_totalMessage.data(),orgdataLength); // send the string dataEE
std::cout << "wrote :" << _totalMessage << std::endl;
}
为什么当我将_totalMessage转换为C字符串以通过插座发送时,它没有发送。
当我尝试将_totalMessage.c_str()打印到屏幕上时,它什么也没打印?这是上面的输出。如您所见,"写作:"行没有显示一些内容。我觉得我缺少将此存档转换为角色数组以发送我的插座的东西?
writing :
Size to write: 38
wrote :22 serialization::archive 12 0 0 0 0
有趣的是,准备要发送的字符串的函数实际上会输出" Outbound.c:"下面:
void aTodo::SendBack(protocolBaseServer & in){
std::ostringstream archive_stream;
{
boost::archive::text_oarchive archive(archive_stream);
archive << *this;
}
archive_stream.flush();
std::string outbound_data=archive_stream.str();
//type needs to be first for factory to create correct object
std::cout << "outbound: " << outbound_data << std::endl;
std::cout << "outbound.c: " << outbound_data.c_str() << std::endl;
outbound_data=_initType+outbound_data;
//now just send it along us
in.DoWrite(outbound_data);
}
编辑。我似乎找到了罪魁祸首。我想对出站字符串进行炭化,所以我这样做了:_intype是const char = 0x01。
似乎您无法使用 运算符将const char串联到字符串中。它会弄乱.c_str()返回。
void aTodo::SendBack(protocolBaseServer & in){
std::ostringstream archive_stream;
{
boost::archive::text_oarchive archive(archive_stream);
archive << *this;
}
archive_stream.flush();
std::string outbound_data=archive_stream.str();
//type needs to be first for factory to create correct object
std::cout << "outbound: " << outbound_data << std::endl;
std::cout << "outbound.c: " << outbound_data.c_str() << std::endl;
outbound_data=_initType+outbound_data;
//now just send it along us
in.DoWrite(outbound_data);
}
我发现了。我想的那样,我的_intype变量没有设置为0x01。它设置为0x00。
也许您需要在使用结果之前关闭存档:
{
boost::archive::text_oarchive archive(archive_stream);
archive << *this;
}
archive_stream.flush();
std::string outbound_data=archive_stream.str();
相关文章:
- 将无符号char*转换为std::istream*C++
- 为什么这个函数将"const char*"转换为"void* const"而不是"const void*"
- 将函数参数"const char*"转换为"std::string_view"是
- C ++如何在使用"tolower"时将char转换为int
- 有没有办法在C++的赋值中将"char**"转换为"double"?
- JavaCPP 错误:无法将参数"1"的"char*"转换为"Abc*
- 为什么从 char 转换为 std::byte 可能是未定义的行为?
- 如何在虚幻引擎4中将char*转换为TCHAR?
- C++:将模板类型 char 转换为 std::string
- strcmp/char* 转换无法按预期工作
- C++中的SQLite char*转换
- 无法从 const char* 转换为 const char *&
- 在 C++ 中将 char 转换为 int,而无需符号位传播
- 如果必须包含西里尔字符,如何将 char* 转换为 std::string?
- 试图使用strCMP时的char转换错误
- 无法从'const char *'转换为'LPCTSTR'
- 将 std::vector<char> 转换为字符* 会导致字符缺陷
- 使用 static_cast 将 char 转换为 int c++
- 如何在 C++(MFC) 中将 char* 转换为 LPCTSTR
- 无法将const char转换为字符串构造字符串