sergelhr
Newbie
Karma: +1/-0
Offline
Posts: 2
|
![](http://nefariousmotorsports.com/forum/Themes/nefmoto/images/post/xx.gif) |
« Reply #126 on: January 20, 2025, 08:50:58 AM »
|
|
|
i was struggling for weeks to unsterdand this thread, finally cs1 works ,an example in python.
import struct
def get_crc32_eeprom(data: bytes, crc_initial: int, start_offset: int = 0) -> str: # crc = crc_initial poly = 0xEDB88320
# Process data as DWORDs (4 bytes at a time), starting at the given offset for i in range(start_offset, len(data), 4): # Ensure there's enough data left for a full DWORD if i + 4 > len(data): break
# Read DWORD (4 bytes) in little-endian order dword = struct.unpack_from('<I', data, i)[0] # Print the DWORD being processed print(f"Processing DWORD at offset {hex(i)}: {hex(dword)}") tmp1 = 0 tmp2 = crc & poly
# Calculate tmp1 using parity bits for j in range(32): tmp1 ^= (tmp2 >> j) & 1 # Update CRC crc = dword ^ ((crc << 1) | tmp1)
# Return CRC as a hex string return hex(crc)
# Example usage if __name__ == "__main__": # Data block from the example data_block = bytes.fromhex( "08 00 6C BA 84 55 FF FF D7 FF 1E 57 E6 9A FF BA" "45 7B CD 6C CC D7 86 C9 58 0A 01 48 6C 2B 5C 39" "92 27 D5 8F AA 80 01 00 57 56 57 5A 5A 5A 41 55" "5A 46 57 35 33 39 35 34 32 A5 D4 8D 54 37 86 86" "8E 69 97 9A 7E 24 87 19 D7 9A FF 7C FC FA 85 E9" "95 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00" "00 00 00 00 00 00 00 00 00 00 00 00 00 54 9D DE" "FC 63 FE B7 CA 09 75 F2 6D 42 00 00 F8 C8 7F 9C" ) crc_initial = 0x0008 start_offset = 0x04 # Start calculation at offset 0x04
# Calculate and print the CRC32 checksum crc_result = get_crc32_eeprom(data_block, crc_initial, start_offset) # print(f"CRC32 last two bytes reversed: {crc_result}")
|
|
|
Logged
|
|