ブール値の文字列があり、これらのブール値をビットとして使用してバイナリ ファイルを作成したいと考えています。これは私がやっていることです:
# first append the string with 0s to make its length a multiple of 8
while len(boolString) % 8 != 0:
boolString += '0'
# write the string to the file byte by byte
i = 0
while i < len(boolString) / 8:
byte = int(boolString[i*8 : (i+1)*8], 2)
outputFile.write('%c' % byte)
i += 1
ただし、これは一度に 1 バイトの出力を生成し、低速です。それを行うためのより効率的な方法は何ですか?