Concatenate a string as binary bytes

Jaime Fernández jjjaime at gmail.com
Tue Dec 14 14:50:41 EST 2010


Hi

To build a binary packet (for SMPP protocol), we have to concatenate
different types of data: integers, floats, strings.

We are using struct.pack to generate the binary representation of each
integer and float of the packet, and then they are concatenated with the +
operand.
However, for strings we directly concatenate the string with +, without
using struct.

Everything works with python 2 except when string encoding is introduced.
Whenever, a non ASCII char appears in the string, an exception is launched.
In python 3, it's not possible to do this trick because all the strings are
unicode.

What would be the best approach to:
 - Support non-ascii chars (we just want to concatenate the binary
representation of the string without any modification)
 - Compatibility between python 2 and python 3.

Thanks,
Jaime
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20101214/79e99ec5/attachment-0001.html>


More information about the Python-list mailing list