我有一些数据缓冲区,一个PyByteArray
,我想从中提取char *
。然后我想获取这个char*并从中创建一个stringstream
void CKKSwrapper::DeserializeContext(PyObject *obj) {
// Is a PyByteArray object as determined by https://docs.python.org/3/c-api/bytearray.html#c.PyByteArray_Check
PyObject_Print(obj, stdout, Py_PRINT_RAW); // 1
const char *s = PyByteArray_AsString(obj); // 2
std::cout << "Converted bytearray to string" << std::endl; // 3
std::cout << s << std::endl; // 4
std::istringstream is(s); // 5
lbcrypto::CryptoContext<lbcrypto::DCRTPoly> new_cc; // 6
Serial::Deserialize(new_cc, is, SerType::BINARY); // 7
}
第二行const char *s = PyByteArray_AsString(obj);
输出一个字符。从前面的问题C++ c_str of std::string returns empty(标题不准确),我知道输入对象的底层数据PyObject *obj
中有NULL
个字符
基于API,我看不到任何即时解决方案。有人有什么建议吗
注意:从当前代码库中,我需要从
Server: (C++ -> Python) -> (sockets) -> Client: (Python -> C++)
所以我真的无法绕过这个物体
如果您阅读the description of all the PyByteArray函数,您将发现一个名为
PyByteArray_Size
的有用函数。这将为您提供数组的大小std::string
有许多构造函数。其中一个从开始迭代器和结束迭代器构造std::string
。指针符合这些条件。因此,您应该能够相应地构造一个完整的std::string
:相关问题 更多 >
编程相关推荐