Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'algorithm': 0.04; 'bytes,': 0.09; 'data:': 0.09; 'python': 0.11; 'def': 0.12; 'translation': 0.12; '(unsigned': 0.16; '1):': 0.16; 'be:': 0.16; 'perfect.': 0.16; 'received:mail-wi0-f178.google.com': 0.16; 'unsigned': 0.16; '\xc2\xa0if': 0.16; 'wrote:': 0.18; 'code.': 0.18; '<': 0.19; "python's": 0.19; 'import': 0.22; 'to:name :python-list@python.org': 0.22; 'char': 0.24; 'integer': 0.24; '>': 0.26; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'url:mailman': 0.30; 'ctypes': 0.31; 'sep': 0.31; 'void': 0.31; 'regular': 0.32; 'received:209.85.212': 0.32; 'url:python': 0.33; 'trouble': 0.34; 'could': 0.34; 'problem': 0.35; 'received:209.85': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'version:': 0.36; 'url:listinfo': 0.36; 'url:org': 0.36; 'received:209': 0.37; 'implement': 0.38; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'short': 0.38; 'skip:& 20': 0.39; 'to:addr:python.org': 0.39; 'url:mail': 0.40; 'skip:\xc2 10': 0.60; 'success': 0.61; 'skip:* 10': 0.61; 'email addr:gmail.com': 0.63; 'otten': 0.84 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:content-type; bh=3K+SXr36M4rpq2135nakfugmJlEJPWi8AwpQOIGl1WE=; b=DaUkLvMAVqwN74soD78v+rwmeFJ+kZoewvkRFjKJZZofAjaRbHDMjaiP2dziy9Ng/W qtUwpRzk2V83f9eaQAG4UxRjRvEQ3hKJ9jA1DlHOJvgmfCtf6tnJO9AIy2qOrnQ2NFTi +kLCcKtEgjAqIim3uSp/XSQ8MbKcj+UptjtjxN0rzZf13H0C9cxHRSzcHrGbcH5C/tmi FZMSkmvGQbzCsaXdAOtvYGVSMA4cyFlsDmhFkBWT+IyFZ7EqaRsvy8wX5CV8sOH2b3wS 07bwCyRGy5m1e3KkAEHX1DcxH6WZRn+/pZra2YKVOUiUihIraU7cM/HfHxZ3aEv9XpoM cnAA== X-Gm-Message-State: ALoCoQkG+GncjemNSj39GpUyhAIaOLhbhkaI0Ml+WTyVPE1MVh6iWIrjDkrOHDJNgrB2mVFzZ+Ev X-Received: by 10.180.8.230 with SMTP id u6mr15687839wia.24.1409683452976; Tue, 02 Sep 2014 11:44:12 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: <01aa8ab2-cceb-4219-8999-5c66520c128e@googlegroups.com> From: Chris Kaynor Date: Tue, 2 Sep 2014 11:43:52 -0700 Subject: Re: crc algorithm To: "python-list@python.org" Content-Type: multipart/alternative; boundary=f46d0442862875fb6605021981f6 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 181 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1409683461 news.xs4all.nl 2879 [2001:888:2000:d::a6]:56113 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:77448 --f46d0442862875fb6605021981f6 Content-Type: text/plain; charset=UTF-8 Also, depending on the use-case, binascii.crc32 might also work fine: https://docs.python.org/2/library/binascii.html#binascii.crc32 import binascii def calc_crc(data): return binascii.crc32(data) Much simpler. Chris On Tue, Sep 2, 2014 at 11:24 AM, Peter Otten <__peter__@web.de> wrote: > 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. > > -- > https://mail.python.org/mailman/listinfo/python-list > --f46d0442862875fb6605021981f6 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

On Tue, Sep 2, 2014 at 11:24 AM, Peter O= tten <__peter__@web.de> wrote:
dream4soul@gmail.co= m wrote:

> I have trouble to implement crc algorithm in python 3.3
>
> c version work=C2=A0 perfect. I try to use bytes, int and c_types with= out 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){
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0unsigned char *dat_ptr;
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0int loopc;
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0unsigned short crc_dat;
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0unsigned char c_work;
>
>=C2=A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>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0} else {
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0crc_dat >>=3D1;
>
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0}
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0c_work >>=3D1;
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0}
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0}
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return(crc_dat);
> }

A near-literal translation would be:

def calc_crc(data):
=C2=A0 =C2=A0 crc =3D 0
=C2=A0 =C2=A0 for work in data:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 for i in range(8):
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if (crc & 1) ^ (work & 1)= :
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 crc >>=3D 1 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 crc ^=3D 0x8408
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 else:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 crc >>=3D 1 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 work >>=3D 1
=C2=A0 =C2=A0 return crc

I don't see any operation where the "unboundedness" of Python= 's integer type
could be a problem -- but no guarantees.

--
https://mail.python.org/mailman/listinfo/python-list

--f46d0442862875fb6605021981f6--