Jan-23-2018, 06:17 PM
Hello friends.
I try calculate CRC32 from my binary file. But Anythink is wrong.
Here is function to read and print block of bin file. It is working very fine.
Can you help me, how to solve the problem? Thank you.
I try calculate CRC32 from my binary file. But Anythink is wrong.
Here is function to read and print block of bin file. It is working very fine.
def ReadFile(offset, blok): i = -15 filename = "EMPTY.bin" blocksize = blok opts,args = getopt.getopt(sys.argv[1:],'f:b:') for o,a in opts: if o == '-f': filename = a if o == '-b': blocksize = a with open(filename,"rb") as f: f.seek(offset, 1) block = f.read(blocksize) str = "" odsaz = "" for ch in block: str += hex(ord(ch))[2:].zfill(2)+" " if (i % 16 == 0): odsaz += hex(i)[2:].zfill(8) + ": " + str + "\n" str = "" i = i + 1 #vypise do konzole vygenerovany log print odsaz return blockAnd I have finction to calculate CRC32:
custom_crc_table = {} def int_to_bytes(i): return [(i >> 24) & 0xFF, (i >> 16) & 0xFF, (i >> 8) & 0xFF, i & 0xFF] def generate_crc32_table(_poly): global custom_crc_table for i in range(256): c = i << 24 for j in range(8): c = (c << 1) ^ _poly if (c & 0x80000000) else c << 1 custom_crc_table[i] = c & 0xffffffff def custom_crc32(buf): global custom_crc_table crc = 0xffffffff for integer in buf: b = int_to_bytes(integer) for byte in b: crc = ((crc << 8) & 0xffffffff) ^ custom_crc_table[(crc >> 24) ^ byte] return crcAnd problem is this. I have this main:
crc.generate_crc32_table(poly) data = ReadFile(0, 1024) custom_crc = crc.custom_crc32(int(data))And I get error:
- File "C:\Users\jirka\Desktop\CRC32\test.py", line 63, in <module>
custom_crc = crc.custom_crc32(int(data))
ValueError: invalid literal for int() with base 10: ''
Can you help me, how to solve the problem? Thank you.