将数据块分成固定长度的块,然后添加 space 并再次将它们全部添加为字符串
chunk of data into fixed lengths chunks and then add a space and again add them all as a string
我得到的十六进制值为 a85b080040010000
。我希望它成为 a8 5b 08 00 40 01 00 00
。我已经使用下面的代码完成了。但我必须处理非常大的数据。所以我希望计算时间非常短。
import binascii
import re
filename = 'calc.exe'
with open(filename, 'rb') as f:
content = f.readline()
text = binascii.hexlify(content)
text1 = binascii.unhexlify(text)
length1 = 32
length2 = 16
list = re.findall('.{%d}' % length1, text)
list1 = re.findall('.{%d}' % length2, text1)
d = []
for i in range (0, len(list), 1):
temp = ""
l = re.findall('.{%d}' % length2, list[i])
s = l[0]
t = iter(s)
temp += str(' '.join(a+b for a,b in zip(t, t)))
temp += " "
s = l[1]
t = iter(s)
temp += str(' '.join(a+b for a,b in zip(t, t)))
temp += " | " + list1[i]
print temp
你可以简单地做
x="a85b080040010000"
print re.sub(r"(.{2})",r" ",x)
或
x="a85b080040010000"
print " ".join([i for i in re.split(r"(.{2})",x) if i])
我得到的十六进制值为 a85b080040010000
。我希望它成为 a8 5b 08 00 40 01 00 00
。我已经使用下面的代码完成了。但我必须处理非常大的数据。所以我希望计算时间非常短。
import binascii
import re
filename = 'calc.exe'
with open(filename, 'rb') as f:
content = f.readline()
text = binascii.hexlify(content)
text1 = binascii.unhexlify(text)
length1 = 32
length2 = 16
list = re.findall('.{%d}' % length1, text)
list1 = re.findall('.{%d}' % length2, text1)
d = []
for i in range (0, len(list), 1):
temp = ""
l = re.findall('.{%d}' % length2, list[i])
s = l[0]
t = iter(s)
temp += str(' '.join(a+b for a,b in zip(t, t)))
temp += " "
s = l[1]
t = iter(s)
temp += str(' '.join(a+b for a,b in zip(t, t)))
temp += " | " + list1[i]
print temp
你可以简单地做
x="a85b080040010000"
print re.sub(r"(.{2})",r" ",x)
或
x="a85b080040010000"
print " ".join([i for i in re.split(r"(.{2})",x) if i])