Python Forum
Please help me to convert a function from vb to python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Please help me to convert a function from vb to python
#11
....and I can't understand why.....
Reply
#12
FINALLY!!!! WITH THAT CODE:

def crc16(data, bits=8):
    crc = 0xFFFF
    for op, code in zip(data[0::2], data[1::2]):
        crc = crc ^ int(op+code, 16)
        for bit in range(0, bits):
            if (crc&0x0001)  == 0x0001:
                crc = ((crc >> 1) ^ 0xA001)
            else:
                crc = crc >> 1
    msb = hex(crc >> 8)
    lsb = hex(crc & 0x00FF)
    return lsb + msb
The result is:

>>> crc16('11010003000C')

'0xce0x9f'
But I need to "clean" the result and must be "0E9F"

Where I'm wrong now?
Reply
#13
0E9F or CE9F as you claimed in previous posts?
Reply
#14
(May-23-2017, 12:12 PM)buran Wrote: 0E9F or CE9F as you claimed in previous posts?

I have correct the code now.
Result must be CE9F without "0x"
Reply
#15
def crc16(data, bits=8):
    crc = 0xFFFF
    for op, code in zip(data[0::2], data[1::2]):
        crc = crc ^ int(op+code, 16)
        for bit in range(0, bits):
            if (crc&0x0001)  == 0x0001:
                crc = ((crc >> 1) ^ 0xA001)
            else:
                crc = crc >> 1
    msb = crc >> 8
    lsb = crc & 0x00FF
    return '{:X}{:X}'.format(lsb, msb)

print crc16('11010003000C')
Reply
#16
You are my hero! Thanks a lot!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert a string to a function mikepy 8 2,555 May-13-2022, 07:28 PM
Last Post: mikepy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020