我試圖計算CRC32-C校驗和值。我介紹了很多在線CRC計算器和很多網站來編寫我自己的crc校驗和計算器。我沒有得到預期的結果。從我的研究只有「http://crccalc.com/」給我的預期價值。來自Python的CRC校驗和計算
我看到有有桌子許多方法,不表。等等一切似乎是爲我工作,我上傳簡單的方法程序,我用來計算CRC
POLY_LENGTH = 32
def convertobin(input_message):
input_message = bin(input_message[::-1])
return input_message
def find_xor(polynomial, input_message):
# print polynomial
print input_message
xor = polynomial^input_message
return xor
def find_crc(polynomial, packet):
print len(packet)
input_messageInBin = bin(int(packet,16))[2:]
print input_messageInBin, type(input_messageInBin)
input_messageInBin = input_messageInBin + '0'*31
inputMessageLength = len(input_messageInBin)
firstTime = 1
remainder = 0
startmarker = 0
control = 1
diff = 0
stopmarker = 0
import pdb; pdb.set_trace()
while control:
if len(input_messageInBin) > (startmarker + 32) :
if firstTime:
inputString = input_messageInBin[0:POLY_LENGTH]
stopmarker = POLY_LENGTH - 1
xor = find_xor(polynomial, int(inputString,2))
xor_bits_length = len(bin(xor)) - 2
remainder = str(bin(xor))[2:]
diff = POLY_LENGTH - xor_bits_length
firstTime = 0
startmarker = startmarker + diff - 1
else:
diff_bits = input_messageInBin[stopmarker+1:stopmarker+diff+1]
inputString = remainder + diff_bits
print "--------------------------------------------------------"
print "Total number of different bits are: ", diff
print "remainder + diff_bits : ", remainder, "+", diff_bits
print "XOR of :"
print inputString
print bin(polynomial)[2:]
xor = find_xor(polynomial, int(inputString,2))
xor_bits_length = len(bin(xor)) - 2
remainder = str(bin(xor))[2:]
print remainder
print "--------------------------------------------------------"
diff = POLY_LENGTH - xor_bits_length
stopmarker = stopmarker + diff
startmarker = startmarker + diff
else:
control = 0
remainder = remainder + input_messageInBin[startmarker:stopmarker]
print stopmarker
print startmarker
return remainder
val = find_crc(0x1EDC6F41, 'FFFFFFFFFFFFFFFFFFFF0F0FB01013F2E8FAF0421208')
print val
我想得到這個工作。欣賞有人是否有這個解決方案
接近答案沒有什麼,但是您應該檢查導入時的[PEP8](https://www.python.org/dev/peps/pep-0008/#imports)。他們應該在文件的開頭,沒有任何有效的理由將它們包含在函數(或類中)中。它並沒有真正受到傷害,但仍然是一個不...... [This](https://stackoverflow.com/a/5262532/1878539)這裏的答案解釋了一點。 – Kraay89
[binascii.crc32](https://docs.python.org/3/library/binascii.html)是否適用於您的目的? – gerardw