Guest User

Untitled

a guest
Jan 17th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. # first append the string with 0s to make its length a multiple of 8
  2. while len(boolString) % 8 != 0:
  3. boolString += '0'
  4.  
  5. # write the string to the file byte by byte
  6. i = 0
  7. while i < len(boolString) / 8:
  8. byte = int(boolString[i*8 : (i+1)*8], 2)
  9. outputFile.write('%c' % byte)
  10.  
  11. i += 1
  12.  
  13. b = bytearray([int(boolString[x:x+8], 2) for x in range(0, len(boolString), 8)])
  14. outputFile.write(b)
  15.  
  16. bitstring.Bits(bin=boolString).tofile(outputFile)
  17.  
  18. while data > 0:
  19. data, byte = divmod(data, 0xff)
  20. file.write('%c' % byte)
  21.  
  22. import array
  23.  
  24. buffer = array.array('B')
  25.  
  26. i = 0
  27. while i < len(boolString) / 8:
  28. byte = int(boolString[i*8 : (i+1)*8], 2)
  29. buffer.append(byte)
  30. i += 1
  31.  
  32. f = file(filename, 'wb')
  33. buffer.tofile(f)
  34. f.close()
  35.  
  36. class BitWriter:
  37. def __init__(self, f):
  38. self.acc = 0
  39. self.bcount = 0
  40. self.out = f
  41.  
  42. def __del__(self):
  43. self.flush()
  44.  
  45. def writebit(self, bit):
  46. if self.bcount == 8 :
  47. self.flush()
  48. if bit > 0:
  49. self.acc |= (1 << (7-self.bcount))
  50. self.bcount += 1
  51.  
  52. def writebits(self, bits, n):
  53. while n > 0:
  54. self.writebit( bits & (1 << (n-1)) )
  55. n -= 1
  56.  
  57. def flush(self):
  58. self.out.write(chr(self.acc))
  59. self.acc = 0
  60. self.bcount = 0
  61.  
  62. with open('outputFile', 'wb') as f:
  63. bw = BitWriter(f)
  64. bw.writebits(int(boolString,2), len(boolString))
  65. bw.flush()
  66.  
  67. def long_to_bytes(n, blocksize=0):
  68. """long_to_bytes(n:long, blocksize:int) : string
  69. Convert a long integer to a byte string.
  70.  
  71. If optional blocksize is given and greater than zero, pad the front of the
  72. byte string with binary zeros so that the length is a multiple of
  73. blocksize.
  74. """
  75. # after much testing, this algorithm was deemed to be the fastest
  76. s = ''
  77. n = long(n)
  78. pack = struct.pack
  79. while n > 0:
  80. s = pack('>I', n & 0xffffffffL) + s
  81. n = n >> 32
  82. # strip off leading zeros
  83. for i in range(len(s)):
  84. if s[i] != '00':
  85. break
  86. else:
  87. # only happens when n == 0
  88. s = '00'
  89. i = 0
  90. s = s[i:]
  91. # add back some pad bytes. this could be done more efficiently w.r.t. the
  92. # de-padding being done above, but sigh...
  93. if blocksize > 0 and len(s) % blocksize:
  94. s = (blocksize - len(s) % blocksize) * '00' + s
  95. return s
  96.  
  97. with open('outputFile', 'wb') as f:
  98. f.write(long_to_bytes(int(boolString, 2)))
  99.  
  100. import struct
  101.  
  102. p = struct.pack('????', True, False, True, False)
  103. assert p == 'x01x00x01x00'
  104. with open("out", "wb") as o:
  105. o.write(p)
  106.  
  107. $ ls -l out
  108. -rw-r--r-- 1 lutz lutz 4 Okt 1 13:26 out
  109. $ od out
  110. 0000000 000001 000001
  111. 000000
  112.  
  113. with open("out", "rb") as i:
  114. q = struct.unpack('????', i.read())
  115. assert q == (True, False, True, False)
Add Comment
Please, Sign In to add comment