我有一种情况,我正在执行一些项目的二进制序列化,并将它们写入一个不透明的字节缓冲区:
int SerializeToBuffer(unsigned char* buffer)
{
stringstream ss;
vector<Serializable> items = GetSerializables();
string serializedItem("");
short len = 0;
for(int i = 0; i < items.size(); ++i)
{
serializedItem = items[i].Serialize();
len = serializedItem.length();
// Write the bytes to the stream
ss.write(*(char*)&(len), 2);
ss.write(serializedItem.c_str(), len);
}
buffer = reinterpret_cast<unsigned char*>(
const_cast<char*>(ss.str().c_str()));
return items.size();
}
从结果中删除const
-nessss.str().c_str()
然后将其分配给缓冲区是否安全?reinterpret_cast
unsigned char*
注意:代码只是为了让您了解我在做什么,它不一定编译。