четверг, 12 апреля 2012 г.

python ctypes usage

With python ctypes arrays, bytes pointers etc. can be scripted. 
In python 2.7 ctypes can be used like this:


from ctypes import *
def crc16(data, numBytes):
crc16 = c_uint16(0)
crc16l = c_uint8(0xFF)
crc16h = c_uint8(0xFF)
symbol = c_uint8()
for counter in range(numBytes):
symbol = c_uint8(data[counter])
symbol.vaule ^= crc16h.value
symbol.value ^= (symbol.value >> 4)
crc16h.value = crc16l.value ^ (symbol.value << 4) ^
                 symbol.value >> 3) 
crc16l.value = symbol.value ^ (symbol.value << 5)
crc16.value |= crc16l.value
crc16.value <<= 8
crc16.value |= crc16h.value
return crc16
if __name__ == '__main__':
data = (c_uint8 * 5)()
data[0] = 0
data[1] = 1
data[2] = 2
data[3] = 3
data[4] = 4
print hex(crc16(data, data.__len__()).value)


Main thing with xor and shift is x.value, without it error appears:
symbol ^= crc16h.value
TypeError: unsupported operand type(s) for ^=: 'c_ubyte' and 'int'

Комментариев нет:

Отправить комментарий