菜農Python菜鳥學習筆記class學習

#! -*- coding:utf-8 -*-
#Python菜鳥類學習
#菜農[email protected] 2019.4.16
class HotCRC(object):
    def __init__(self):
        self.polystr = "CRC16L_1021_FFFF_0000"
        self.polynomial = "CRC16=X16+X12+X5+1"
        self.bits = 16 
        self.dir = 1
        self.init = 0xFFFF
        self.poly = 0x1021
        self.xorout = 0x0000
    def getPoly(self):
        return self.poly
    def inttohex(self, val, size):
        return "{:>0{}X}".format(val, size)[-size:]
    def hextoint(self, string):
        return int(string, 16)
    def getcrcformat(self, crcstr, index = 0):
        rstr = ""
        cstr = crcstr.split("_")
        l = len(cstr)
        if (l == 4) and (index < l):
            rstr = cstr[index]
        return rstr
    def getcrcbits(self, crcstr):
        bits = 0
        cstr = crcstr.split("_")
        if len(cstr) == 4:
            cstr = cstr[0]
            if cstr[:3] == "CRC" and (cstr[-1] == "L" or cstr[-1] == "R"):
                bits = int(cstr[3:-1])
        return bits
    def getcrcdir(self, crcstr):
        dir = ""
        cstr = crcstr.split("_")
        if len(cstr) == 4:
            dir = cstr[0][-1]
        return dir
        
crc = HotCRC()
print(crc.inttohex(crc.poly, 4))
crcstr = "CRC16R_8005_0000_FFFF"
print(crc.inttohex(0x123,2))
print(crc.hextoint("123"))
print(crc.getcrcformat(crcstr))
print(crc.getcrcformat(crcstr, 1))
print(crc.getcrcbits(crcstr))
print(crc.getcrcdir(crcstr))

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章