我尝试将二进制文件中的数据转换为一和零的字符串。二进制文件的大小约为2MB,将其全部转换需要花费大量时间,如何使它更快?
我已经尝试通过将字节转换为int并将其从那里转换为字符串来将过程分割为几部分
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)))
我的解决方法是:
"".join([format(ord(byte), "08b") for byte in bytes])
尽管,我不知道它有多快。