如何从 boost::p ython::list 中提取值到C++
How to extract values from boost::python::list to C++
我正在Linux中创建.so文件,以便我可以将它们导入python脚本并开始使用它们。我需要将数据从python传递到c ++层,以便我可以使用它们。尽管参考了许多帖子,但我无法提取值。我在下面给出了参考代码。u8 => 无符号字符
#include "cp2p_layer.h"
#include <boost/python.hpp>
using namespace boost::python;
BOOST_PYTHON_MODULE(cp2p_hal)
{
class_<SCSICommandsB>("SCSICommandsB")
.def("Write10", &SCSICommandsB::Write10)
;
}
以下代码来自cp2p_layer.cpp。我可以获取列表的长度,但数据始终是黑色的
u16 SCSICommandsB::Write10 (u8 lun, u8 config, u32 LBA, u16 transferLen, u8 control, u8 groupNo, boost::python::list pythonList)
{
u16 listLen;
u8* pDataBuf = new u8[transferLen];
listLen = boost::python::len(pythonList);
if( listLen != transferLen)
{
cout<<"nwarning: The write10 cdb has transfer length "<<transferLen<<"that doesnt match with data buffer size "<<listLen<<"n";
}
for(int i = 0; i < listLen; i++)
{
pDataBuf[i] = boost::python::extract<u8>( (pythonList)[i] );
cout<<boost::python::extract<u8>( (pythonList)[i] )<<"-";
//cout<<pDataBuf[i]<<".";
}
cout<<"n";
cout<<"info: inside write10 boost len:"<<listLen<<"n";
return oScsi.Write10 (lun, config, LBA, transferLen, control, groupNo, pDataBuf);
}
当我执行 python 脚本时
#!/usr/bin/python
import cp2p_hal
scsiCmds = cp2p_hal.SCSICommandsB()
plist = [0,1,2,3,4,5,6,7,8,9]
print len(plist)
scsiCmds.Write10(0,0,0,10,0,0,plist)
输出为
10
-------- -
info: inside write10 boost len:10
任何帮助都非常感谢。我也有关于如何在执行读取命令后从 c++ 层读取数据的问题。完成此操作后,我将创建一个新帖子。提前谢谢。
问题仅在于打印值。 C++中的u8
是一个unsigned char
,cout
将输出相应的ASCII字符。 您的字符 (0-9) 不可打印,ASCII 9 除外,它恰好是一个制表符。 这解释了输出中最后一个连字符之前的空格。
如何解决? 在输出之前转换为 int:
cout << static_cast<int>(boost::python::extract<u8>(pythonList[i])) << "-";
相关文章:
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 如何从 std::atomic 中提取指针 T<T>?
- 为什么istream不支持右值提取
- 如何设置一个范围来提取我想要获得的信息
- 视觉工作室项目.提取源文件夹名称
- C++17 - 使用自定义分配器的节点提取/重新插入 - 适用于 clang++/libc++,但不适用于 libstd
- 从字符串中提取整数并形成一个数组
- C ++中的StringStream有助于使用向量从字符串中提取逗号分隔的整数,而不是空格分隔的整数,为什么?
- asn1c 不会从 asn.1 模块中提取八位字节字符串的默认值
- 从 std::vector<无符号字符>切片中提取 int?
- 在C++中使用重载提取运算符时出现问题
- 在 Qt(C++) 中使用 QProcess 解压缩 - 提取目录问题
- C++(.cpp文件和.h文件)拆分代码并添加一个函数,提取 - 这很容易吗?
- 无法从 std::string 中提取C++ Unicode 符号
- 如何从也包含C++字母的文本文件中提取某些数字?
- 如何从适合一定范围的数字中提取数字?
- C++从通用引用中提取实际类型
- 从运行服务的应用程序代码中提取窗口句柄
- 如何在字符串中找到字符,然后在C++中提取其余的字符串
- std::unordered_map::提取引用/指针失效