This is a creation in Article, where the information may have evolved or changed. Packet in the network transmission must be considered in case of data corruption, CRC32 and Adler32 are the most commonly used algorithms.
Both go and Android are built into the implementation of these two algorithms, and the direct use is good.
The Go method is called as follows:
Validation algorithm (ADLER32/CRC32) example//author:xiong Chuan liang//date:2015-4-12package mainimport ("FMT" "Hash/adler32" "hash/ Crc32 ") var ADLER32 int = 0var CRC32 int = 1func Main () {for _, V: = range []string{" Aaaaaaaaaa "," 3333SDFSDFFSDFFSD "," 234 esrewr234324 ", ' an Adler-32 checksum was obtained by calculating" 16-bit checksums A and B and concatenating their bits into a 32-bit integer. A is the sum of any bytes in the stream plus one, and B are the sum of the individual values of a from each step. At the beginning of A Adler-32 Run, A was initialized to 1, and B to 0. The sums is done modulo 65521 (the largest prime number smaller than 216). The bytes is stored in network order (big endian), and B occupying the most significant bytes. The function may be expressed AsA = 1 + D1 + D2 + ... + Dn (mod 65521) B = (1 + D1) + (1 + D1 + D2) + ... + (1 + D1 + D2 + ... + dn) (mod 65521) = nxD1 + (n−1) xD2 + (n−2) xD3 + ... + DN + N (mod 65521) Adler-32 (d) = Bx65536 + Awhere D is the String of bytes for whicH The checksum is to being calculated, and n is the length of D. '} {Calc (ADLER32, []byte (v)) Calc (CRC32, []byte (v))}}func Calc (t int, b []byte) {var ret uint32if ADLER32 = = t {ret = Adler32. Checksum ([]byte (b)) fmt. Printf ("ADLER32%15d:%s ... \ n", ret, String (B[:5]))} else if CRC32 = = = T {ret = CRC32. Checksumieee ([]byte (b)) fmt. Printf ("CRC32%15d:%s ... \ n", ret, String (b[:5))} else {return}}
Android is not an example, the relevant package is described in detail:
CRC32:
Http://wear.techbrood.com/reference/java/util/zip/CRC32.html
Adler32:
Http://wear.techbrood.com/reference/java/util/zip/Adler32.html
Both have the same effect, but Adler32 are relatively small in terms of computation.
blog:http://blog.csdn.net/xcl168