0
我正在編寫一個程序來計算Groovy中的CRC32。出於某種原因,我不明白(因爲如果我用java.util.zip
執行相同的)期望值:CRC32計算與默認值不匹配
def crc32(byte[] bytes) {
return new java.util.zip.CRC32().with { update bytes; value }
}
def myCrc32(byte[] bytes) {
def remainder = 0x0
def multiple = 0
def poly = 0xEDB88320
bytes.each { b ->
remainder ^= b
for (int i = 0; i < 8; i++) {
multiple = (remainder & 1) ? poly : 0;
remainder = (remainder >> 1)^multiple;
}
}
return remainder
}
def origFile = 'file'
def fileBytes = new File(origFile).text.getBytes()
def origRes = crc32(fileBytes)
def myRes = myCrc32(fileBytes)
println origRes
println myRes
在哪裏我錯了嗎?我用下面的消息人士的指導:
結果,我得到:
1838399800 - original
4005013284 - my calculation