简体   繁体   English

将 C_ulong_Array_40 转换为 python 中的列表或字符串?

[英]Convert C_ulong_Array_40 to list or string in python?

I'm trying to fight through some python code that interfaces with an hardware unit.我试图通过一些与硬件单元接口的 python 代码进行斗争。

The unit have an python lib where I use an function called "readsub".该单元有一个 python 库,我在其中使用了一个名为“readsub”的 function。 But it returns the data in a format I get as "C_ulong_Array_40"但它以我得到的格式返回数据“C_ulong_Array_40”

The code is simple:代码很简单:

err, length, data = card.ReadSub(2)
write_tofile(1, data)

But then I get the following error: unsupported operand types (s) for +: 'c_ulong_Array_40' and 'str'但后来我收到以下错误:+ 的不支持的操作数类型:'c_ulong_Array_40' and 'str'

I assume I need to use ctypes for something, but it's all a bit of "magic" right now since I'm not that good in python yet.我假设我需要使用 ctypes 做某事,但现在这有点“神奇”,因为我在 python 中还不是那么好。

What way can I convert the "data" to some format usefull?我可以通过什么方式将“数据”转换为某种有用的格式? it can either be to an python list or one long string containing all data.它可以是一个 python 列表或一个包含所有数据的长字符串。

Thanks, br.谢谢,兄弟。 Martin马丁

From what I just learnt C_ulong_Array_40 is a ctype array of datatype ulong with a length of 40.从我刚刚学到的C_ulong_Array_40是一个长度为 40 的数据类型ulong的 ctype 数组。

FYI c_ulong is alias for c_uint , also c_long is alias for c_int https://docs.python.org/3/library/ctypes.html ).仅供参考c_ulong是 c_uint 的别名, c_uint也是c_long的别名c_int ://docs.python.org/3/library/ctypes.ZFC35FDC70D5FC69D269883A822 )

To get your list of Integers;获取您的整数列表;

list_of_ints = [x for x in data]

To get your list of Strings;获取您的字符串列表;

list_of_str_ints = [str(x) for x in data]

No strange conversions needed!不需要奇怪的转换!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM