Python Forum

Full Version: Please help me to convert a function from vb to python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
....and I can't understand why.....
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?
0E9F or CE9F as you claimed in previous posts?
(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"
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')
You are my hero! Thanks a lot!
Pages: 1 2