Karl P
Karl P

Reputation: 413

efficiently turning a ctypes LP_c_ubyte into a python 'str'

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

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?

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)

This works, but it's pretty ugly.

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.

Are there any better ways?

Upvotes: 2

Views: 3169

Answers (1)

ralight
ralight

Reputation: 11608

I think this should do what you want:

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

Upvotes: 5

Related Questions