crc algorithm

dream4soul at gmail.com dream4soul at gmail.com
Tue Sep 2 19:50:17 CEST 2014


Dear all,

I have trouble to implement crc algorithm in python 3.3

c version work  perfect. I try to use bytes, int and c_types without any success
can some who help me:

c version:

unsigned short calc_crc(const void *p_dat, int l_dat){
        unsigned char *dat_ptr;
        int loopc;
        unsigned short crc_dat;
        unsigned char c_work;

        dat_ptr = (unsigned char*)p_dat;
        crc_dat = 0x0000;
        for (; l_dat > 0; l_dat--)
        {
                c_work = *(dat_ptr++);
                for (loopc = 0; loopc < 8; loopc++)
                {
                        if ((((unsigned char )(crc_dat & 0x0001)) ^ (c_work & 0x01)) == 0x01)
                        {
                                crc_dat >>=1 ;
                                crc_dat ^=0x8408;
                        } else {
                                crc_dat >>=1;

                        }
                        c_work >>=1;
                }
        }
        return(crc_dat);
}


python tries:

def calc_crc():
        crc_dat = c_ushort(0x0001)


        data = [0x00,0x00,0x34,0x35,0x38,0x35]
        for x in range(len(data)):
                pass
                c_work = c_ubyte(data[x])
                #print(c_work)
                for x in range(8):
                        pass
                        if (c_ubyte(crc_dat.value & c_ushort(0x0001).value).value ^ c_ubyte(c_work.value & c_ubyte(0x01).value).value) == c_ubyte(0x01):
                                crc_dat.value >>=1
                                crc_dat.value ^=0x8408

                        else:
                                crc_dat.value >>=1

                        c_work.value >>=1
                        print(crc_dat)

        print(crc_dat.value)
        pass





More information about the Python-list mailing list