如何将大量数据从二进制文件快速转换为一和零的字符串

问题描述 投票:0回答:1

我尝试将二进制文件中的数据转换为一和零的字符串。二进制文件的大小约为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)))
python string binaryfiles converters
1个回答
0
投票

我的解决方法是:

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

尽管,我不知道它有多快。

© www.soinside.com 2019 - 2024. All rights reserved.