need help with converting c function to python function

nephish nephish at gmail.com
Thu Jul 5 13:32:08 EDT 2007


hello all,

i have a c function from some modbus documentation that i need to
translate into python.

it looks like this:


unsigned short CRC16(puchMsg, usDataLen)
unsigned char *puchMsg ;
unsigned short usDataLen ;
{
   unsigned char uchCRCHi = 0xFF ;
   unsigned char uchCRCLo = 0xFF ;
   unsigned uIndex ;
   while (usDataLen--)
       {
       uIndex = uchCRCHi ^ *puchMsgg++ ;
       uchCRCHi = uchCRCLo ^ auchCRCHi[uIndex} ;
       uchCRCLo = auchCRCLo[uIndex] ;
       }
   return (uchCRCHi << 8 | uchCRCLo) ;
}

some of it i can make out, but i can't seem to figgure out
this part ' auchCRCHi[uIndex};
it looks like a typo, because there is a closing } that does not match
the opening [.


here is what i have so far, but is not giving me the right values

 def crc16(data):
     crc_hi = 0xff
     crc_lo = 0xff
     for x in data:
         crc_index = crc_hi ^ x
         crc_hi = crc_lo ^ (crc_hi | crc_index)
         crc_lo = crc_lo | crc_index
     return (crc_hi << 8 | crc_lo)

whaddya think?

thanks




More information about the Python-list mailing list