简体   繁体   中英

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

I have a file which contains binary data. The content of this file is just one long line.
Example: 010101000011101010101
Originaly the content was an array of c++ objects with the following data types:

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

I want to skip var1 and var3 and only extract the values of var2 into some readable decimal values. My idea was to read the file byte by byte and convert them into hex values. In the next step I though I could "combine" (append) 4 of those hex values to get one int32 value.
Example: 0x10 0xAA 0x00 0x50 -> 0x10AA0050

My code so far:

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

Now I have the problem that my append_hex is not working because the variables are strings so the bitshift is not working.

I am new to python so please give me hints when I can do something in a better way.

You can use struct module for reading binary files.

This can help you Reading a binary file into a struct in Python

A character can be converted to a int using the ord(x) method. In order to get the integer value of a multi-byte number, bitshift left. For example, from a earlier project:

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

Note this code assumes big-endian system, you will need to reverse the index for parsing little-endian code.

If you know exaclty what the size of the struct is going to be, (or can easily calculate it based on the size of the file) you are probably better of using the " struct " module.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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