DarkKnight35
DarkKnight35

Reputation: 61

How to convert a lot of data from binary file to string of ones and zeros fast

I try to convert the data in binary file to string of ones and zeros. The size of the binary file is about 2MB which right takes a lot of time to convert it all, how can I make it faster

I have tried to cut the process to pieces by converting the bytes to int and from there to string

def bytes_to_string(self, xbytes):
    intermediate_result = int.from_bytes(xbytes, byteorder='big')
    return intermediate_result


def dec_to_bin(self,x):
    return int(bin(x)[2:])

##
## bin_code is where the data from the binary file is stored

bin_code = str(self.dec_to_bin(self.bytes_to_string(bin_code)))

Upvotes: 0

Views: 158

Answers (1)

My Solution would be:

"".join([format(ord(byte), "08b") for byte in bytes])

Although, I dont know how fast it is.

Upvotes: 1

Related Questions