[Tutor] help with translating a c function to a python function
Alan Gauld
alan.gauld at btinternet.com
Thu Jul 5 23:22:03 CEST 2007
"shawn bright" <nephish at gmail.com> wrote
> oh, 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
So far so good..
> crc_hi = crc_lo ^ (crc_hi | crc_index)
But the C code here is:
> uchCRCHi = uchCRCLo ^ auchCRCHi[uIndex} ;
> crc_lo = crc_lo | crc_index
now I assume the a in front indicates an array - which
since its not defined locally must be global somewhere.
And they are indexing it So your code should be something
like:
crc_hi = crc_lo ^ CRC_hiList[crc_index]
> uchCRCLo = auchCRCLo[uIndex] ;
and
crc_lo = CRC_loList[crc_index]
return (crc_hi << 8 | crc_lo)
> whaddya think?
I think I'm guessing quite a lot and that your C function is
not truly standalone. :-)
But I think the corrupt operations are indexes into an array.
But where the array is defined is anyones guess.
And in C a pointer can also be an array so auchCRCHi
could be defined as
SOMETYPE *auchCRCHi
or
SOMETYPE auchCRCHi[n]
HTH,
Alan G.
On 7/5/07, shawn bright <nephish at gmail.com> wrote:
> 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 [.
>
> anyway, if someone could kinda give me a push, it would help me out
> a lot.
> thanks
>
_______________________________________________
Tutor maillist - Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list