mercredi 22 avril 2015

Python - Function transforms a number from the 7 system to the binary system

I want to create a function f(n), which directly transform a number from one system to the another. My code is working but is not directly.

def g(s):
   nr=208957 # my index number
   m1=nr%8+2
   m2=nr%7+2 # the binary system
   tmp1=0
   result=[]
   s1 = [int(i) for i in s[::-1]]
   for i in range(len(s1)):
      tmp1+=s1[i]*m1**i
   while (tmp1):
      result.append(tmp1%m2)
      tmp1=tmp1//m2
   r = ''.join([str(i) for i in result[::-1]])
   return r
g(s)

Aucun commentaire:

Enregistrer un commentaire