[Tutor] Concatenating numeric data in Python 3.3
Rob Day
robert.day at merton.oxon.org
Wed Apr 24 19:21:01 CEST 2013
So, for example, you'd be trying to concatenate the binary data
01010101 and 10101010, and get 0101010110101010? You can do that by
shifting the bits:
>>> (0b01010101 << 8) + 0b10101010
21930
which is equivalent to:
>>> 0b0101010110101010
21930
You can also do it with numerical data:
>>> 0b10101010
170
>>> 0b01010101
85
>>> (85 << 8) + 170
21930
>>> "{:b}".format((85 << 8) + 170)
'101010110101010'
On 24 April 2013 17:52, sparkle Plenty <sparkle.plenty12481632 at gmail.com> wrote:
> What is the best way to concatenate packed numeric data? I am building a
> message to send to a device and it has a very specific header format and
> variable length payload. Converting to string, concatenating, and then
> converting back to numeric introduced errors. The tuple() function also
> introduced errors.
>
> The code is proprietary so I am not comfortable posting it. I have been
> programming in Python for a few weeks. It is my first OOP language. My
> background, in the dim and distant past, is mainframe.
>
> Any help would be greatly appreciated. Thank you in advance.
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
--
Robert K. Day
robert.day at merton.oxon.org
More information about the Tutor
mailing list