def getBytesFromBits(bitsStream):
 if (not isinstance(bitsStream, str)):
  return ((-1), 'The bitsStream must be a string')
 bytes = ''
 if re.match('[01]*$', bitsStream):
  try:
   for i in range(0, len(bitsStream), 8):
    bits = bitsStream[i:(i + 8)]
    byte = chr(int(bits, 2))
    bytes += byte
  except:
   return ((-1), 'Error in conversion from bits to bytes')
  return (0, bytes)
 else:
  return ((-1), 'The format of the bit stream is not correct')

def getBytesFromBits ( bitsStream ) : if ( not isinstance ( bitsStream , str ) ) : return ( ( - 1 ) , 'The bitsStream must be a string' ) bytes = '' if re . match ( '[01]*$' , bitsStream ) : try : for i in range ( 0 , len ( bitsStream ) , 8 ) : bits = bitsStream [ i : ( i + 8 ) ] byte = chr ( int ( bits , 2 ) ) bytes += byte except : return ( ( - 1 ) , 'Error in conversion from bits to bytes' ) return ( 0 , bytes ) else : return ( ( - 1 ) , 'The format of the bit stream is not correct' )
makes the conversion between bits and bytes .