我想按6个字节顺序读取二进制文件的十六进制数

I want to read the hexadecimal number of a binary file sequentially by 6 bytes

import binascii
import sys
from os.path import getsize

target = './'+sys.argv[1]

with open(target,'rb+') as f:
    file_size = getsize(target)
    string1 = f.read(6)
    
    print("Size : %d"%file_size)
    print (binascii.b2a_hex(string1))

我有一个大小为 1220 字节的文件。 忽略前 20 个字节 我想从第21个字节开始输出6个字节。

例如,


00 00 00 00 00 ... 00 00 00 ( 20bytes 0x00 ) AA BB CC DD EE FF
GG HH II JJ KK LL[=19 的十六进制数据=]MM ...

我的预期输出:
AA BB CC DD EE FF
GG HH II JJ KK LL
毫米.... .. .. 我想如图所示,一个一个地加载和输出6个字节。

我为此编写了代码来获取文件大小。 但我不太清楚在那之后该怎么做。 所以我寻求帮助。

截至目前,您的代码读取了前 6 个字节。要在读取之前跳过 20 个字节,可以使用 seek() 方法 [1]. To display the hexadecimal values seperated by a space you can use the bytes.hex function [2]。将所有内容放在一起,您可以使用以下内容:

import binascii
import sys
from os.path import getsize

target = './'+sys.argv[1]

with open(target,'rb+') as f:
    file_size = getsize(target)
    
    # read 6 bytes from position 20
    f.seek(20)
    string1 = f.read(6)

    print("Size : %d"%file_size)
    print (string1.hex(' ',1))