__author__ = 'rony' from ctypes import BigEndianStructure from ctypes import sizeof, memmove, addressof from ctypes import c_int32, c_uint32 from struct import pack, unpack class BigEndianByteStructure(BigEndianStructure): # https://wiki.python.org/moin/ctypes @classmethod def from_bytes(cls, ibytes): return_val = cls() if len(ibytes) != sizeof(return_val): raise ValueError( '{0} Cannot unpack {2} bytes, it should be {1} bytes'.format(cls.__name__, sizeof(return_val), len(ibytes))) memmove(addressof(return_val), ibytes, len(ibytes)) return return_val @classmethod def from_structure(cls, original): return_val = cls() for field in return_val._fields_: try: setattr(return_val, field[0], getattr(original, field[0])) except: print 'Attribute {0} not found in original'.format(field[0]) return return_val # POSITIVE # IEEE754 HEX 0x428beb2a # AN575 HEX 0x850beb2a # NEGATIVE # IEEE754 HEX 0xc28beb2a # AN575 HEX 0x858beb2a class IEEE754Float(BigEndianByteStructure): _pack_ = 1 _fields_ = [('sign', c_int32, 1), ('exponent', c_int32, 8), ('number', c_int32, 23)] @classmethod def from_float(cls, f): return cls.from_bytes(pack('>f', f)) def __float__(self): return unpack('>f', buffer(self))[0] def debug(self): f = self return hex(f.exponent), hex(f.number), f.sign, float(f), [hex(b) for b in bytearray(f)] class AN575Float(BigEndianByteStructure): _pack_ = 1 _fields_ = [('exponent', c_int32, 8), ('sign', c_int32, 1), ('number', c_int32, 23)] @classmethod def from_float(cls, f): return cls.from_structure(IEEE754Float.from_float(f)) def __float__(self): a = IEEE754Float.from_structure(self) return float(a) def debug(self): f = self return hex(f.exponent), hex(f.number), f.sign, float(f), [hex(b) for b in bytearray(f)] class IEEE754Float_u(BigEndianByteStructure): _pack_ = 1 _fields_ = [('sign', c_uint32, 1), ('exponent', c_int32, 8), ('number', c_uint32, 23)] @classmethod def from_float(cls, f): return cls.from_bytes(pack('>f', f)) def __float__(self): return unpack('>f', buffer(self))[0] def debug(self): f = self return hex(f.exponent), hex(f.number), f.sign, float(f), [hex(b) for b in bytearray(f)] class AN575Float_uint(BigEndianByteStructure): _pack_ = 1 _fields_ = [('exponent', c_int32, 8), ('sign', c_uint32, 1), ('number', c_int32, 23)] @classmethod def from_float(cls, f): return cls.from_structure(IEEE754Float_u.from_float(f)) def __float__(self): a = IEEE754Float_u.from_structure(self) return float(a) def debug(self): f = self return hex(f.exponent), hex(f.number), f.sign, float(f), [hex(b) for b in bytearray(f)] print '\n', 'c_int32 TESTS:' print 'Class Name\t\t', 'exponent', '\tnumber', '\tSign', '\tfloat', '\t\t\t\t', 'binary repr' f = IEEE754Float.from_float(69.9593067) print type(f).__name__, f.debug() f = IEEE754Float.from_bytes('\x42\x8b\xeb\x2a') print type(f).__name__, f.debug() f = AN575Float.from_float(69.9593067) print type(f).__name__, f.debug() f = AN575Float.from_structure(IEEE754Float.from_float(69.9593067)) print type(f).__name__, f.debug() f = AN575Float.from_bytes('\x85\x0b\xeb\x2a') print type(f).__name__, f.debug() f = IEEE754Float.from_bytes('\xc2\x8b\xeb\x2a') print type(f).__name__, f.debug() f = AN575Float.from_bytes('\x85\x8b\xeb\x2a') print type(f).__name__, f.debug() print '\n', 'c_uint32 TESTS:' print 'Class Name\t\t', 'exponent', '\tnumber', '\tSign', '\tfloat', '\t\t\t\t', 'binary repr' f = IEEE754Float_u.from_float(69.9593067) print type(f).__name__, f.debug() f = IEEE754Float_u.from_bytes('\x42\x8b\xeb\x2a') print type(f).__name__, f.debug() f = AN575Float_uint.from_float(69.9593067) print type(f).__name__, f.debug() f = AN575Float_uint.from_structure(IEEE754Float.from_float(69.9593067)) print type(f).__name__, f.debug() f = AN575Float_uint.from_bytes('\x85\x0b\xeb\x2a') print type(f).__name__, f.debug() f = IEEE754Float_u.from_bytes('\xc2\x8b\xeb\x2a') print type(f).__name__, f.debug() f = AN575Float_uint.from_bytes('\x85\x8b\xeb\x2a') print type(f).__name__, f.debug()