why is this thread marked as solved D:
this isn't solved until this function is complete:
def __BIT(big,signed,byte_size,value):
if type(value)==str:
if value=='': #Read
p=__POS()
strval=f.read(byte_size)
if len(strval)==byte_size: #check for EOF
sv=(reversed(strval) if big else strval)
val=0
for i,v in enumerate(sv): val=val|(int(v.encode('hex'),16)<<(i*8)) #int
if signed == 1: val=(val-pow(256,byte_size) if val>(pow(256,byte_size)/2) else val) #signed
if signed == 2: #float (IEEE)
e=((byte_size*8)-1)//(byte_size+1)+(byte_size>2)*byte_size//2; m,b=((byte_size*8)-(1+e)),int('1'*(e-1),2)
S,E,M=[(val>>((byte_size*8)-1))&1,(val>>m)&int('1'*e,2),val&int('1'*m,2)] #<- added brackets (faster processing)
if E == int('1'*e,2): val=(float('NaN') if M!=0 else (float('+inf') if S else float('-inf')))
else: val=((pow(-1,S)*(2**(E-b-m)*((1<<m)+M))) if E else pow(-1,S)*(2**(1-b-m)*M))
#credit to pyTony for simplifying the formula of 'e' and fixing the return values
__LOG(p+': read 0x'+strval.encode('hex')+' as '+str(val))
return val
elif type(value)==int: #write int
if signed==1: value=(value+pow(256,byte_size) if value<0 else value)
Bytes=[chr((value>>(8*i))&255) for i in range(byte_size)]
f.write(''.join(reversed(Bytes)) if big else ''.join(Bytes))
elif type(value)==float: #write float
#----------------------------------------
#TODO: need float2hex conversion aglorithm
from struct import pack
if byte_size==4: f.write(pack(('<f' if big else '>f'), value)[0:4])
if byte_size==8: f.write(pack(('<d' if big else '>d'), value)[0:4])
#----------------------------------------
elif type(value)==list: return list(__BIT(big,signed,byte_size,Lval) for Lval in value)
elif type(value)==tuple: return tuple(__BIT(big,signed,byte_size,Tval) for Tval in value)
elif type(value)==bool: return (__BIT(big,signed,byte_size,int(value)))
@ Gribouillis: what I mean is the returned length of the float from __BIT() is greater than that from struct.unpack()
btw, that function does everything from reading and writing ints, signed-ints, and floats to/from the file
(of any specified byte length)
if anyone has any speedups or anything, it would be much appreciated. :)