How to pack a string variable of length 1 as a char using struct.pack?

danwgrace at gmail.com danwgrace at gmail.com
Tue Aug 5 08:15:41 EDT 2014


Hi,
How to pack a string variable of length 1 as a char using struct.pack?
The following works fine:
p = struct.pack('c', b'1')

Whereas this causes an error "char format requires a bytes object of length 1":
s = '1'
p = struct.pack('c', s)

I need to pack a variable rather than a literal.

Thanks.



More information about the Python-list mailing list