Effectively turning ctypes LP_c_ubyte into python 'str'

I'm having trouble ctypes binding and ctypes docs lighten my head up a bit.

I have a remote network client sending binary data, and the library used (Mosquitto, for MQTT message brokers) provides a ctypes method to get the source binary data from the network. This is the type "LP_c_ubyte". Is there an efficient way to turn this back into a python 'str' object?

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

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

It works, but it's pretty ugly.

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

Are there any better ways?

+1
source share
1 answer

I think this should do what you want:

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

Source: https://habr.com/ru/post/1607851/


All Articles