2016-11-08 8 views
0

私はGroovyでCRC32を計算するプログラムを書いています。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 

が、私は間違いをしました:何らかの理由で私は(私はjava.util.zip実装を使用する場合と同じ)の期待値を得ることはありませんか?私はガイドとして、以下のソースを使用:私が得る

結果:

1838399800 - original 
4005013284 - my calculation 

答えて

1

OK]をクリックして、自分自身をこれを考え出しました。

1)ベースとしてremainderjava.util.zip.Crc32は0xFFFFFFFを使用します。

2)ライブラリは、実際にビットを反転してから、0xFFFFFFFと排他的論理和をとります。だから基本的に私はreturnステートメントに同じXORを追加し、正解を得ました。

関連する問題