Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed3a.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'algorithm': 0.04; 'bytes,': 0.09; 'data:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'def': 0.12; 'translation': 0.12; '(unsigned': 0.16; '1):': 0.16; 'be:': 0.16; 'perfect.': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'unsigned': 0.16; 'wrote:': 0.18; 'code.': 0.18; "python's": 0.19; 'header:User-Agent:1': 0.23; 'char': 0.24; 'integer': 0.24; 'header:X-Complaints-To:1': 0.27; 'ctypes': 0.31; 'void': 0.31; 'regular': 0.32; 'trouble': 0.34; 'could': 0.34; 'problem': 0.35; 'but': 0.35; 'version': 0.36; 'version:': 0.36; 'implement': 0.38; 'to:addr:python-list': 0.38; 'short': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'success': 0.61; 'skip:* 10': 0.61; 'email addr:gmail.com': 0.63 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: crc algorithm Date: Tue, 02 Sep 2014 20:24:54 +0200 Organization: None References: <01aa8ab2-cceb-4219-8999-5c66520c128e@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bdb54c.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 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: 56 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1409682316 news.xs4all.nl 2880 [2001:888:2000:d::a6]:51061 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:77447 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.