Advertisement
paulogp

ConvertBinaryStringIntoByteList

Aug 7th, 2011
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.40 KB | None | 0 0
  1. # paulogp
  2. def ConvertBinaryStringIntoByteList(texto):
  3.     comprimento = len(texto)
  4.     Lista = []
  5.     byte = 0
  6.     bits = 0
  7.  
  8.     for i in range(0,comprimento):
  9.         byte = (byte<<1) | int(texto[i])
  10.         if bits<7:
  11.             bits = bits + 1
  12.         else:
  13.             Lista.append(byte)
  14.             byte = 0
  15.             bits = 0
  16.  
  17.     if bits!=0:
  18.             Lista.append(byte<<(8-bits))
  19.     return Lista
  20.  
  21. # exemplo
  22. print(ConvertBinaryStringIntoByteList('1024'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement