Vedere il titolo. Il valore restituito è 32 bit, giusto? Perché non restituire un int?Perché java.util.zip.CRC32.getValue() restituisce un valore long, non un int?
8
A
risposta
9
Perché se restituisse un int, metà del CRC sarebbe negativo. L'aspettativa è che un CRC a 32 bit non sia firmato, vale a dire 0,4294967295, che non può essere rappresentato in un int.
2
java.util.zip.CRC32
implementa l'interfaccia Checksum
, che richiede un tipo long
cambio getValue()
, richiedendo così un long
per un checksum a 32 bit; i 32 bit superiori dell'output sono quasi definitivamente 0.
Mark, grazie per la risposta. Ho un seguito: sto leggendo e scrivendo file i cui ultimi byte sono un CRC di tutti i byte precedenti. Il CRC deve essere contenuto in 4 byte. Quindi non posso ignorare i 4 byte superiori del CRC lungo, posso? Avrei bisogno di convertire la rappresentazione lunga e una "non firmata", in primo luogo, giusto? – Steveo
Per definizione, un CRC a 32 bit si adatta ai quattro byte bassi del lungo. Non c'è alcun tipo di 32 bit senza segno in Java. –