[Tutor] 7bit - 8bit

Kent Johnson kent37 at tds.net
Tue May 9 14:14:45 CEST 2006

János Juhász wrote:
> Dear All,
> I have to convert a binary stream from a monitoring device to another 
> format.
> The highets bit of  data bytes is replaced by 0 and placed after every 7 
> bytes into a  correction byte.
> I would like to decode it as simple as possible.
> May someone suggest an elegant solution for that ?

By decode, do you mean go from the 7bit representation to the 8-bit binary?

A few things you may need:
To convert a string to a list of integer values use a list comprehension 
and ord():
[ ord(c) for c in s ]
or map():
map(ord, s)

To extract or set bits use the bitwise logical operators & and |

To assemble a list of integers back into a string use chr(), list comp 
or map(), and join():
''.join([chr(b) for b in bytes])
''.join(map(chr, bytes))


More information about the Tutor mailing list