繁体   English   中英

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

[英]Reading binary file into different hex “types” (8bit, 16bit, 32bit, …)

我有一个包含二进制数据的文件。 该文件的内容只是一个长行。
示例: 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的问题是我的append_hex无效,因为变量是字符串,所以append_hex不起作用。

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

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

这可以帮助您将二进制文件读取到Python中的结构中

可以使用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 ”模块。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM