Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #77445 > unrolled thread
| Started by | dream4soul@gmail.com |
|---|---|
| First post | 2014-09-02 10:50 -0700 |
| Last post | 2014-09-02 23:25 -0700 |
| Articles | 12 — 4 participants |
Back to article view | Back to comp.lang.python
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
| From | dream4soul@gmail.com |
|---|---|
| Date | 2014-09-02 10:50 -0700 |
| Subject | crc algorithm |
| Message-ID | <01aa8ab2-cceb-4219-8999-5c66520c128e@googlegroups.com> |
Dear all,
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:
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);
}
python tries:
def calc_crc():
crc_dat = c_ushort(0x0001)
data = [0x00,0x00,0x34,0x35,0x38,0x35]
for x in range(len(data)):
pass
c_work = c_ubyte(data[x])
#print(c_work)
for x in range(8):
pass
if (c_ubyte(crc_dat.value & c_ushort(0x0001).value).value ^ c_ubyte(c_work.value & c_ubyte(0x01).value).value) == c_ubyte(0x01):
crc_dat.value >>=1
crc_dat.value ^=0x8408
else:
crc_dat.value >>=1
c_work.value >>=1
print(crc_dat)
print(crc_dat.value)
pass
[toc] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2014-09-02 20:24 +0200 |
| Message-ID | <mailman.13713.1409682316.18130.python-list@python.org> |
| In reply to | #77445 |
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.
[toc] | [prev] | [next] | [standalone]
| From | dream4soul@gmail.com |
|---|---|
| Date | 2014-09-02 23:19 -0700 |
| Message-ID | <d843096a-d1a3-4817-b78f-f5ebeb7b4d43@googlegroups.com> |
| In reply to | #77447 |
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
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2014-09-03 09:19 +0200 |
| Message-ID | <mailman.13729.1409728782.18130.python-list@python.org> |
| In reply to | #77465 |
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 <stdio.h>
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
[toc] | [prev] | [next] | [standalone]
| From | dream4soul@gmail.com |
|---|---|
| Date | 2014-09-03 01:43 -0700 |
| Message-ID | <9e5c2de2-5c96-468d-84b4-3db511117ea0@googlegroups.com> |
| In reply to | #77469 |
On Wednesday, September 3, 2014 10:19:29 AM UTC+3, Peter Otten wrote:
> 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 <stdio.h>
>
>
>
> 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
int main(int argc, char const *argv[])
{
unsigned short rez;
unsigned char a[]={0x30,0x30,0x34,0x35,0x38,0x35};
unsigned short val;
rez=calc_crc(a,(int)sizeof(a));
printf("%#hx\n",rez );
return 0;
}
[toc] | [prev] | [next] | [standalone]
| From | dream4soul@gmail.com |
|---|---|
| Date | 2014-09-03 01:46 -0700 |
| Message-ID | <c6816d5f-e76e-4dd9-8a09-44fefcb82365@googlegroups.com> |
| In reply to | #77469 |
On Wednesday, September 3, 2014 10:19:29 AM UTC+3, Peter Otten wrote:
> 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 <stdio.h>
>
>
>
> 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
int main(int argc, char const *argv[])
{
unsigned short rez;
unsigned char a[]={0x30,0x30,0x34,0x35,0x38,0x35};
unsigned short val;
rez=calc_crc(a,(int)sizeof(a));
printf("%#hx\n",rez );
return 0;
}
o$ gcc main.c
o$ ./a.out
0x8c40
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2014-09-03 11:00 +0200 |
| Message-ID | <mailman.13731.1409734825.18130.python-list@python.org> |
| In reply to | #77475 |
dream4soul@gmail.com wrote:
> calc_crc(b'\x00\x00\x34\x35\x38\x35')
> unsigned char a[]={0x30,0x30,0x34,0x35,0x38,0x35};
The first two bytes differ; you made an error on the input.
[toc] | [prev] | [next] | [standalone]
| From | dream4soul@gmail.com |
|---|---|
| Date | 2014-09-03 04:00 -0700 |
| Message-ID | <b3e60c31-bf75-4726-b048-d4e0d8971f2b@googlegroups.com> |
| In reply to | #77476 |
On Wednesday, September 3, 2014 12:00:10 PM UTC+3, Peter Otten wrote:
> dream4soul@gmail.com wrote:
>
>
>
> > calc_crc(b'\x00\x00\x34\x35\x38\x35')
>
>
>
> > unsigned char a[]={0x30,0x30,0x34,0x35,0x38,0x35};
>
>
>
> The first two bytes differ; you made an error on the input.
Dear Peter, my apologies it's my mistake. Thank you for help. Problem solved.
[toc] | [prev] | [next] | [standalone]
| From | dream4soul@gmail.com |
|---|---|
| Date | 2014-09-03 07:31 -0700 |
| Message-ID | <8304747e-ef5c-4e73-a2b6-1c54071ef35c@googlegroups.com> |
| In reply to | #77476 |
On Wednesday, September 3, 2014 12:00:10 PM UTC+3, Peter Otten wrote:
> dream4soul@gmail.com wrote:
>
>
>
> > calc_crc(b'\x00\x00\x34\x35\x38\x35')
>
>
>
> > unsigned char a[]={0x30,0x30,0x34,0x35,0x38,0x35};
>
>
>
> The first two bytes differ; you made an error on the input.
Dear Peter, my apologies it's my mistake. Thank you for help. Problem solved
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2014-09-03 08:22 +0100 |
| Message-ID | <mailman.13730.1409728987.18130.python-list@python.org> |
| In reply to | #77465 |
On 03/09/2014 07:19, dream4soul@gmail.com wrote: Would you please access this list via https://mail.python.org/mailman/listinfo/python-list or read and action this https://wiki.python.org/moin/GoogleGroupsPython to prevent us seeing double line spacing and single line paragraphs, thanks. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Chris Kaynor <ckaynor@zindagigames.com> |
|---|---|
| Date | 2014-09-02 11:43 -0700 |
| Message-ID | <mailman.13714.1409683461.18130.python-list@python.org> |
| In reply to | #77445 |
[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
>
[toc] | [prev] | [next] | [standalone]
| From | dream4soul@gmail.com |
|---|---|
| Date | 2014-09-02 23:25 -0700 |
| Message-ID | <1d54dfb1-a4da-4924-b5a6-661e74d76820@googlegroups.com> |
| In reply to | #77448 |
On Tuesday, September 2, 2014 9:43:52 PM UTC+3, Chris Kaynor wrote:
> 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 <__pe...@web.de> wrote:
>
>
>
> dream...@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
this doesn't work binascii.crc32(data) return 32 bit data , c function crc return 16 bit and it is not standard crc
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web