将二进制文件读入不同的十六进制“类型”(8位,16位,32位,...)

问题描述 投票:4回答:2

我有一个包含二进制数据的文件。该文件的内容只是一个长行。 示例:010101000011101010101 原始内容是具有以下数据类型的c ++对象数组:

// Care pseudo code, just for visualisation
int64 var1;
int32 var2[50];
int08 var3;

我想跳过var1var3,只将var2的值提取为一些可读的十进制值。我的想法是逐字节读取文件并将它们转换为十六进制值。在下一步我虽然我可以“组合”(追加)这些十六进制值中的4个来获得一个int32值。 示例:0x10 0xAA 0x00 0x50 -> 0x10AA0050 我的代码到目前为止:

def append_hex(a, b):
    return (a << 4) | b

with open("file.dat", "rb") as f:
    counter = 0
    tickdifcounter = 0
    current_byte=" "
    while True:
        if (counter >= 8) and (counter < 208):
            tickdifcounter+=1
            if (tickdifcounter <= 4):
                current_byte = append_hex(current_byte, f.read(1))
                if (not current_byte):
                    break
                val = ord(current_byte)
        if (tickdifcounter > 4):
            print hex(val)
            tickdifcounter = 0
            current_byte=""
        counter+=1
        if(counter == 209):    #209 bytes = int64 + (int32*50) + int08
            counter = 0
    print

现在我遇到的问题是我的append_hex无效,因为变量是字符串,所以bitshift不起作用。

我是python的新手,所以当我能以更好的方式做某事时,请给我提示。

python binaryfiles
2个回答
1
投票

您可以使用struct module来读取二进制文件。

这可以帮助你Reading a binary file into a struct in Python


1
投票

可以使用ord(x)方法将字符转换为int。为了获得多字节数的整数值,将bitshift保留为左。例如,从早期的项目:

def parseNumber(string, index):
    return ord(string[index])<<24 + ord(string[index+1])<<16 + \
           ord(string[index+2])<<8+ord(string[index+3])

请注意,此代码假定为big-endian系统,您需要反转索引以解析little-endian代码。

如果您确切知道结构的大小,(或者可以根据文件的大小轻松计算它),那么最好使用“struct”模块。

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