Convert variable directly into a string (no ASCII)

Günther Dietrich guenther.dietrich at spamfence.net
Sat May 2 14:33:25 EDT 2009


Justin Rajewski <Justin at EmbeddedMicro.com> wrote:

>I need to print variables out over serial, however I need them to not be
>in ASCII, ie if the variable is 5 then print 5 not "5".
>
>The function that writes to the serial port requires a string and I can
>send non-variables out with the string "/x05" for 5.

See module struct:

>>> struct.pack('b', 5)
'\x05'
>>> struct.pack('BBBB', 5, 150, 3, 224)
'\x05\x96\x03\xe0'



Regards,

Günther



More information about the Python-list mailing list