Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!ecngs!feeder2.ecngs.de!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4.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; '#include': 0.09; 'bytes,': 0.09; 'data:': 0.09; 'main()': 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; 'skip:" 20': 0.27; 'header:X -Complaints-To:1': 0.27; 'function': 0.29; "doesn't": 0.30; 'gives': 0.31; 'ctypes': 0.31; 'gcc': 0.31; 'void': 0.31; 'skip:c 30': 0.32; '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; 'otten': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: crc algorithm Date: Wed, 03 Sep 2014 09:19:29 +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: p57bd8c81.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: 164 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1409728782 news.xs4all.nl 2883 [2001:888:2000:d::a6]:43090 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:77469 dream4soul@gmail.com wrote: > On Tuesday, September 2, 2014 9:24:54 PM UTC+3, Peter Otten 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. > > this doesn't work > > calc_crc(b'\x00\x00\x34\x35\x38\x35') > rsult 0x9f41 , but c function gives us 0x8c40 Are you sure? I get 0x9f41 with the C version you posted: $ cat crc.c #include 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); } main() { unsigned char data[] = "\x00\x00\x34\x35\x38\x35"; unsigned short crc = calc_crc(data, 6); printf("%x\n", crc); } $ gcc crc.c $ ./a.out 9f41