Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #77448

Re: crc algorithm

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 <ckaynor@zindagigames.com>
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; '&lt;': 0.19; "python's": 0.19; 'import': 0.22; 'to:name :python-list@python.org': 0.22; 'char': 0.24; 'integer': 0.24; '&gt;': 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 <lu521n$1vs$1@ger.gmane.org>
References <01aa8ab2-cceb-4219-8999-5c66520c128e@googlegroups.com> <lu521n$1vs$1@ger.gmane.org>
From Chris Kaynor <ckaynor@zindagigames.com>
Date Tue, 2 Sep 2014 11:43:52 -0700
Subject Re: crc algorithm
To "python-list@python.org" <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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.13714.1409683461.18130.python-list@python.org> (permalink)
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

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

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
>

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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