Reading a binary file with python

Read the binary file content like this: with open(fileName, mode=”rb”) as file: # b is important -> binary fileContent = file.read() then “unpack” binary data using struct.unpack: The start bytes: struct.unpack(“iiiii”, fileContent[:20]) The body: ignore the heading bytes and the trailing byte (= 24); The remaining part forms the body, to know the number of … Read more

How to convert string to binary?

Something like this? >>> st = “hello world” >>> ‘ ‘.join(format(ord(x), ‘b’) for x in st) ‘1101000 1100101 1101100 1101100 1101111 100000 1110111 1101111 1110010 1101100 1100100’ #using `bytearray` >>> ‘ ‘.join(format(x, ‘b’) for x in bytearray(st, ‘utf-8’)) ‘1101000 1100101 1101100 1101100 1101111 100000 1110111 1101111 1110010 1101100 1100100’

Fast way of counting non-zero bits in positive integer

For arbitrary-length integers, bin(n).count(“1”) is the fastest I could find in pure Python. I tried adapting Óscar’s and Adam’s solutions to process the integer in 64-bit and 32-bit chunks, respectively. Both were at least ten times slower than bin(n).count(“1”) (the 32-bit version took about half again as much time). On the other hand, gmpy popcount() … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)