怪异的CRC校验码

jinhucn
Posts: 20
Joined: Wed Dec 23, 2015 12:06 pm

怪异的CRC校验码

Postby jinhucn » Sat Apr 01, 2017 9:07 am

$SDK\tool\gen_appbin.py中计算了flash_bin_name的crc校验码然后附加到文件末尾。
其中算法如下:
all_bin_crc = getFileCRC(flash_bin_name)
print all_bin_crc
if all_bin_crc < 0:
all_bin_crc = abs(all_bin_crc) - 1
else :
all_bin_crc = abs(all_bin_crc) + 1
print all_bin_crc
write_file(flash_bin_name,chr((all_bin_crc & 0x000000FF))+chr((all_bin_crc & 0x0000FF00) >> 8)+chr((all_bin_crc & 0x00FF0000) >> 16)+chr((all_bin_crc & 0xFF000000) >> 24))
这个算法当all_bin_crc<0时是正确的,整个文件可以得到0xFFFFFFFF的校验码。
当all_bin_crc>0时则得到各种不同的校验码,必须改成all_bin_crc = -(abs(all_bin_crc) + 1)才能得到正确的0xFFFFFFFF校验码。
或者直接改成
all_bin_crc = getFileCRC(flash_bin_name)
print all_bin_crc
all_bin_crc = ~all_bin_crc
print all_bin_crc
write_file(flash_bin_name,chr((all_bin_crc & 0x000000FF))+chr((all_bin_crc & 0x0000FF00) >> 8)+chr((all_bin_crc & 0x00FF0000) >> 16)+chr((all_bin_crc & 0xFF000000) >> 24))

Who is online

Users browsing this forum: No registered users and 5 guests