crc algorithm

dream4soul at gmail.com dream4soul at gmail.com
Wed Sep 3 10:43:37 CEST 2014


On Wednesday, September 3, 2014 10:19:29 AM UTC+3, Peter Otten wrote:
> dream4soul at gmail.com wrote:
> 
> 
> 
> > On Tuesday, September 2, 2014 9:24:54 PM UTC+3, Peter Otten wrote:
> 
> >> dream4soul at gmail.com wrote:
> 
> >> 
> 
> >> 
> 
> >> 
> 
> >> > 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:
> 
> >> 
> 
> >> 
> 
> >> 
> 
> >> ctypes is for interfacing with C; don't use it in regular code.
> 
> >> 
> 
> >> 
> 
> >> 
> 
> >> > 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);
> 
> >> 
> 
> >> > }
> 
> >> 
> 
> >> 
> 
> >> 
> 
> >> A near-literal translation would be:
> 
> >> 
> 
> >> 
> 
> >> 
> 
> >> def calc_crc(data):
> 
> >> 
> 
> >>     crc = 0
> 
> >> 
> 
> >>     for work in data:
> 
> >> 
> 
> >>         for i in range(8):
> 
> >> 
> 
> >>             if (crc & 1) ^ (work & 1):
> 
> >> 
> 
> >>                 crc >>= 1
> 
> >> 
> 
> >>                 crc ^= 0x8408
> 
> >> 
> 
> >>             else:
> 
> >> 
> 
> >>                 crc >>= 1
> 
> >> 
> 
> >>             work >>= 1
> 
> >> 
> 
> >>     return crc
> 
> >> 
> 
> >> 
> 
> >> 
> 
> >> I don't see any operation where the "unboundedness" of Python's integer
> 
> >> type
> 
> >> 
> 
> >> could be a problem -- but no guarantees.
> 
> > 
> 
> > this doesn't work
> 
> > 
> 
> > calc_crc(b'\x00\x00\x34\x35\x38\x35')
> 
> > rsult 0x9f41 , but c function gives us 0x8c40
> 
> 
> 
> Are you sure? I get 0x9f41 with the C version you posted:
> 
> 
> 
> $ cat crc.c
> 
> #include <stdio.h>
> 
> 
> 
> 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);
> 
> }
> 
> 
> 
> main()
> 
> {
> 
>   unsigned char data[] = "\x00\x00\x34\x35\x38\x35";
> 
>   unsigned short crc = calc_crc(data, 6);
> 
>   printf("%x\n", crc);
> 
> }
> 
> $ gcc crc.c
> 
> $ ./a.out 
> 
> 9f41

int main(int argc, char const *argv[])
{
        unsigned short rez;
        unsigned char a[]={0x30,0x30,0x34,0x35,0x38,0x35};
  
        unsigned short val;
 
        rez=calc_crc(a,(int)sizeof(a));
        printf("%#hx\n",rez );
     
        return 0;
}



More information about the Python-list mailing list