1ère Générale NSI

 

Term. Générale NSI

 

Terminale STI2D SIN

Bts Ccst

Technico-commercial 3.0

from distutils.command.bdist import show_formats
from distutils.spawn import spawn
from email.errors import NoBoundaryInMultipartDefect
import re
from unicodedata import decimal
from xml.dom import NoModificationAllowedErr



class Conversion:
      def __init__(self):
            pass

      def c2to10(self,number2,show=False):
            """
            Conversion binaire vers decimale
            """
            lgnumber2 = len(number2)
            number10 = 0
            showtxt = ""
            showtxt2 = ""
            for i in range(lgnumber2):
                  number10 += int(number2[i])*2**(lgnumber2-i-1)
                  if showtxt != "":
                        showtxt += " + "
                        showtxt2 += " + "
                  showtxt +=   number2[i]+"*2^"+str(lgnumber2-i-1) 
                  showtxt2 +=   number2[i]+"*"+str(2**(lgnumber2-i-1)) 
            
            if show:
                  print(showtxt)
                  print(showtxt2)
            print(number10)


      def c10to2(self,number10,show=False):
            """
            Conversion decimale vers binaire 
            """
            number2=""
            espace = ""
            showtxt = ""
            while number10>0:
                  if espace == "":
                        showtxt = str(number10)+"|"+"\033[4m 2\033[0m"
                        if show :
                              print(showtxt)
                        espace = self.__add_spaces(len(str(number10))-1)
                  else:
                        if number10 > 1 :
                              showtxt += " " +str(number10)+"|"+"\033[4m 2\033[0m"
                        else:
                              showtxt += " " +str(number10)
                        espace += self.__add_spaces(len(str(number10))+2)
                        if show :
                              print(showtxt)
                  bit = number10 % 2
                  number2 = str(bit) + number2 
                  number10 = number10 // 2
                  showtxt = espace + str(bit)+"|"
            espace = self.__add_spaces(len(espace)-3)
            if show :
                  print(espace,"<--")      
            print(number2)
            
      def __add_spaces(self,number):
            spaces = ""
            for _ in range(number):
                  spaces += " "
            return spaces

      def c10to2on8b(self,number,show=True):
            print(self.convertentier(number,2**7,show))

      def c10to2on16b(self,number):
            print(self.convertentier(number,2**15,show))

      def convertentier(self,nombre,p2,show):
            maxnombre = len(str(nombre))
            maxp2 = len(str(p2))
         
            str_convert=""
            while p2>=1:
                  if nombre >= p2:
                        #print(p2,"*",1)
                        calcul = nombre - p2
                        if show:
                              self.affichage(str(nombre),str(p2),"1",str(calcul),maxnombre,maxp2)
                        str_convert += "1"
                        nombre -= p2
                  else :
                        #print(p2,"*",0)
                        if show:
                              self.affichage("",str(p2),"0","",maxnombre,maxp2)
                  
                        str_convert += "0"
                  p2 = p2//2
            
            return str_convert
      
      def c_rel_10_to_2(self,number,show=True):
            p2 = 2**7
            while number<-p2 or number>(p2-1) :
                  p2 = p2 * 2**8
            
            if number >= 0 :
                  signe = "0"
                  resultat = self.convertentier(number,p2,show)
                  print(resultat)
            else :
                  signe = "1"
                  print("negatif")
                  resultat = self.convertentier(-number,p2,show)
                  print(resultat)
                  inversion = ""
                  for c in resultat :
                        if c == "0":
                              inversion += "1"
                        else :
                              inversion += "0"
                  print(inversion)
                  relatif2 = signe + self.__add1__(inversion)
                  
                  print(relatif2)



      def __add1__(self,number2):
            posi = len(number2)-1
            retenue = 1
            relatif2 = ""
            while posi>=0 :
                  if number2[posi] == "0" and retenue == 1 :
                        relatif2 =    "1" + relatif2 
                        retenue = 0
                  elif number2[posi] == "1" and retenue == 1 :
                        relatif2 =   "0" + relatif2 
                        retenue = 1
                  else :
                        relatif2 =   number2[posi] + relatif2
                  posi -= 1
            return relatif2[1:] 
            
                  
                  


                         

                  



            

      
      def c_decimal_2(self,number,show=False):
            c2 = self.convertvirgule(number,20,show)
            print("0."+c2)

      
      def convertvirgule(self,nombre,nbbits,show):
            maxp2 = len(str(round(2**(-nbbits),nbbits)))
            maxnombre = maxp2
            str_convert=""
            p2 = 0.5
            while nbbits>0:
                  if nombre >= p2:
                        calcul = nombre - p2
                        calcul = round(calcul,10) 
                        if show:
                              self.affichage(str(round(nombre,10)),str(p2),"1",str(calcul),maxnombre,maxp2)
                        str_convert += "1"
                        nombre -= p2
                        if nombre == 0:
                              return str_convert
                  else :
                        if show:
                              self.affichage("",str(p2),"0","",maxnombre,maxp2)
                        str_convert += "0"
                  p2 = p2/2
                  nbbits-=1
            return str_convert


      def affichage(self,nombre1,p21,bit,calcul,maxnombre,maxp2):
            nbesp1 = maxnombre - len(nombre1)
            esp1 = ""
            nbesp2 = maxp2 - len(p21)
            esp2 = ""
            for _ in range(nbesp1):
                  esp1 += " "
            for _ in range(nbesp2):
                  esp2 += " "
            if nombre1 == "":
                  texte = esp1 + nombre1 +"    " +esp2+ p21+"*"+bit
            else :
                  texte = esp1 + nombre1 +" - " +esp2+ p21+"*"+bit + " = " +calcul
            print(texte)

from bitstring import BitArray
 
 
b = "01001111"
 
x = BitArray(bin=b).int
print(x) 
f1 = BitArray(float=10.625, length=32).hex
f2 = BitArray('float:64=5.4e31').bin
print(f1)
print(f2)

 




conv = Conversion()
conv.c2to10("101")
conv.c10to2(14)
conv.c10to2on8b(14)

conv.c_decimal_2(0.625)
conv.c_rel_10_to_2(127)
conv.c_rel_10_to_2(-128)
conv.c_rel_10_to_2(-1)




En poursuivant votre navigation sur mon site, vous acceptez l’utilisation des Cookies et autres traceurs  pour réaliser des statistiques de visites et enregistrer sur votre machine vos activités pédagogiques. En savoir plus.