Python CRC16 test __python

Source: Internet
Author: User
CRC16 calculation. py
#! /usr/bin/python # _*_ Coding:utf-8 _*_ "" "Simple CRC16 Calculation example: CRC16 Check code calculation method is as follows: 1, initialization CRC value of 0xFFFF 2, the number of checked 1th byte With a 8-bit low or a CRC, put the result into the CRC of the low 8 bit (CRC high 8-bit unchanged) 3, CRC right 1-bit, if the removal of 0 continue to move to the right; 1CRC registers follow the generation of polynomials (value in reverse order) or (x16+x15+x2+1 <--> 0x8005 >>> 0xa0001) 4, repeat steps 3, 8 times the 1th Word Data processing 5, take the next 1 bytes of the test data, for 2, 3, 4 steps; to the entire data test complete 6, the final CRC value, high and low byte Exchange "" "# CR 
    C16 generating polynomial: x16+x15+x2+1 generator_polynomial = 0x8005 # CRC initial value is: 0xFFFF CRC = 0xFFFF def CALCULATECRC (DataArray):    "" To check that the input data is legitimate:p Aram DataArray: Data that needs to be generated for CRC: return: Legal Data Tuples "" "DataList = Dataarray.split () #以空格分割字符串得到对应字符串列表 Print (U ' input string sequence: {0} '. Format (datalist)) index = 0 Try: # converts the input string into data by a different number Sequence for index, item in Enumerate (DataList): # Print Index,item if ' 0x ' in Item.lower (). St RIP (): datalist[index] = Int (item,) Elif ' 0o ' in Item.lower (). Strip (): D Atalist[index] = Int (item,8) elif ' 0b ' in Item.lower (). Strip (): datalist[index] = Int (item,2) Else: 
        Datalist[index] = Int (item) Print (U ' corresponding sequence after successful conversion: {0} '. Format (DataList)) # Processing 1th byte data  temp = calculateonebyte (datalist.pop (0), 0xFFFF) # Recycle other byte data for data in datalist:temp = Calculateonebyte (data,temp) return temp except valueerror as Err:print (U ' {0} data {1} entered incorrectly '. Format (Index,datalist[index]). Encode (' Utf-8 ') print (ERR) # finally: # print (' Current datalist:{0} '. Format (DAT alist) def calculateonebyte (Databyte, TEMPCRC): "" Calculates the CRC value of 1-byte data:p Aram Databyte: Byte data to be computed:p Aram 
    TEMPCRC: Current CRC value: return: The new CRC value "" "# Databyte must be byte data # assert 0x00 <= databyte <= 0xFF # ditto byte data check if not 0x00 <= databyte <= 0xff:raise Exception ((U ' data: 0x{0:<02x} is not byte data [0x00-0xFF] '. Form at (databyte)). Encode (' UTF-8 ') # The Byte data root CRC the current value of the lower 8 bits is different or low_byte = (databyte ^ tempcrc) & 0X00FF # The High 8-bit value of the current CRC is unchanged RESULTCRC = (TEMPCRC & 0xff00) | Low_byte # Loop calculates 8-bit data for index in range (8): # If the minimum is 1:CRC the current value is the same as the generated polynomial or 0 continues if RESULTCRC & 0x 
            0001 = 1: #print ("[%d]: 0x%4x ^^ ^^ 0x%4x"% (index,resultcrc>>1,resultcrc^generator_polynomial)) RESULTCRC >>= 1 RESULTCRC ^= 0xa001 # 0xa001 is the 0x8005 loop right-shift 16-bit value else: # Prin T ("[{0}]: 0x{1:x} >>>> 0x{2:x}". Format (index,resultcrc,resultcrc>>1)) RESULTCRC >>= 1 return RESULTCRC if __name__ = = ' __main__ ': data_string = ' 0x01 2 0o3 ' #以16进制, 10-in, 8-input data stream Prin 
 
 
 
 
 
 T (' data: {0:s} ' corresponds to the CRC16 check code: 0x{1:04x} '. Format (DATA_STRING,CALCULATECRC (data_string))

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.