def read_short(f, endian): """Read a 2-Byte integer from file f with a given endianness (byte order). endian can be either 'big' or 'little'. """ if (endian != 'little') and (endian != 'big'): _logger.debug('File address:', f.tell()) raise ByteOrderError(endian) else: data = f.read(2) # hexadecimal representation if endian == 'big': s = B_short elif endian == 'little': s = L_short return s.unpack(data)[0] # struct.unpack returns a tuple
def read_long(f, endian): """Read a 4-Byte integer from file f with a given endianness (byte order). endian can be either 'big' or 'little'. """ if (endian != 'little') and (endian != 'big'): _logger.debug('File address:', f.tell()) raise ByteOrderError(endian) else: data = f.read(4) if endian == 'big': s = B_long elif endian == 'little': s = L_long return s.unpack(data)[0]
def read_double(f, endian): """Read a 8-Byte floating point from file f with a given endianness (byte order). endian can be either 'big' or 'little'. """ if (endian != 'little') and (endian != 'big'): _logger.debug('File address:', f.tell()) raise ByteOrderError(endian) else: data = f.read(8) if endian == 'big': s = B_double elif endian == 'little': s = L_double return s.unpack(data)[0]