Hi, I have been learning python for the last few months. I was not able to find a function for conversion from a 32-bit Hexadecimal Representation To Decimal Floating-Point. The one included in the library is really tough to use. Hence I have written my own. Kindly go through and acknowledge if u like it or otherwise. def hexconvert32(str_rcv): #string received :str_rcv str_rcv='0x'+str_rcv #appending 0x to it str_bin=bin(int(str_rcv,16)) #converting to binary k=[] #initializing reverse list k for i in range(len(str_bin)-1,-1,-1): k.append(str_bin[i]) k.pop() # popping the '0' k.pop() #popping the 'b' for i in range(len(k),33,1): #padding with 0 k.append('0') sign_bit=k.pop() #popping the sign bit expo='' sini='' for exp in range(30,22,-1): #removing exponent part expo= expo + k[exp] for sin in range(22,-1,-1): #removing significand part sini= sini + k[sin] h=[] dval=0 for i in range(1, len(sini),1): #finding the decimal value of significand dval =dval + (int(sini[i-1])*pow(2,(-1)*(i))) dval=dval+1 # adding 1 to the decimal dval #from here on we are looking at cases for each value. Detailed equations and explanations @ http://www.psc.edu/general/software/packages/ieee/ieee.php if int(expo,2)<255 and int(expo,2)>0 : v=(pow(-1,int(sign_bit,2)))*(pow(2,int(expo,2)-127))*(dval) return v elif int(expo,2)==0 and int(sini,2)!=0: v=(pow(-1,int(sign_bit,2)))*(pow(2,-126))*(dval-1) print v , 'unnormalized value' return 0 elif int(expo,2)==255 and int(sini,2)!=0: print 'Not a Number' return 0 elif int(expo,2)==0 and int(sini,2)==0 and int(sign_bit,2)==1: print '- infinity' return 0 elif int(expo,2)==0 and int(sini,2)==0 and int(sign_bit,2)==0: print 'infinity' return 0 else: print 'error' return 0 Thanks and Regards!! Krishnan G.V
participants (1)
-
GV, Krishnan (GE Transportation, Non-GE)