Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #77447
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Subject | Re: crc algorithm |
| Date | 2014-09-02 20:24 +0200 |
| Organization | None |
| References | <01aa8ab2-cceb-4219-8999-5c66520c128e@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.13713.1409682316.18130.python-list@python.org> (permalink) |
dream4soul@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.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
crc algorithm dream4soul@gmail.com - 2014-09-02 10:50 -0700
Re: crc algorithm Peter Otten <__peter__@web.de> - 2014-09-02 20:24 +0200
Re: crc algorithm dream4soul@gmail.com - 2014-09-02 23:19 -0700
Re: crc algorithm Peter Otten <__peter__@web.de> - 2014-09-03 09:19 +0200
Re: crc algorithm dream4soul@gmail.com - 2014-09-03 01:43 -0700
Re: crc algorithm dream4soul@gmail.com - 2014-09-03 01:46 -0700
Re: crc algorithm Peter Otten <__peter__@web.de> - 2014-09-03 11:00 +0200
Re: crc algorithm dream4soul@gmail.com - 2014-09-03 04:00 -0700
Re: crc algorithm dream4soul@gmail.com - 2014-09-03 07:31 -0700
Re: crc algorithm Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-09-03 08:22 +0100
Re: crc algorithm Chris Kaynor <ckaynor@zindagigames.com> - 2014-09-02 11:43 -0700
Re: crc algorithm dream4soul@gmail.com - 2014-09-02 23:25 -0700
csiph-web