有效地将ctypes LP_c_ubyte转换为python'str' [英] efficiently turning a ctypes LP_c_ubyte into a python 'str'

查看:587
本文介绍了有效地将ctypes LP_c_ubyte转换为python'str'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用ctypes绑定时遇到问题,并且ctypes文档使我的头部有点受伤。

I'm having problems with a ctypes binding, and the ctypes docs are making my head hurt a little.

我有一个远程网络客户端正在发送二进制数据,并且我正在使用的库(Mosquitto,用于MQTT消息代理)提供了一种ctypes方法,用于从网络获取原始二进制数据。这是 LP_c_ubyte类型。有什么有效的方法可以将其转换为python'str'对象?

I have a remote network client sending binary data and the library I'm using (Mosquitto, for MQTT message brokers) provides a ctypes method for getting the original binary data from the network. This is a "LP_c_ubyte" type. Is there any efficient way of turning this back into a python 'str' object?

我需要一组常规的字节才能用于M2Crypto的解密功能。​​

I need a regular set of bytes to use for M2Crypto's decrypt functions.

pp = ''.join(chr(msg.payload[i]) for i in xrange(msg.payloadlen)) 
clear_text = rsa.private_decrypt(pp, M2Crypto.RSA.pkcs1_padding)

这很有效,但是很难看

我可以先将客户端更改为对base64进行编码,然后再对unbase64进行编码,但这似乎也可以解决。

I can go and change the client to base64 encode everything first, and then unbase64 on this end, but that also seems like a bit of a workaround.

还有更好的方法吗?

推荐答案

我认为这应该可以解决您的问题想要:

I think this should do what you want:

import ctypes
pp = ctypes.string_at(msg.payload, msg.payloadlen)

这篇关于有效地将ctypes LP_c_ubyte转换为python'str'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆