Go and Android's Crc32/adler32 algorithm use

Source: Internet
Author: User
Tags crc32

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




Go and Android's Crc32/adler32 algorithm use

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.