1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
| def convertisseur_deci_en_binaire(n):
binaire=''
while(n!=0):
binaire = str(n%2) + binaire
n = n//2
return binaire
def convertisseur_binaire_en_deci(n):
t=0
n=str(n)
s=len(n)
w=0
while(t<s):
u=n[t]
w=(int(u)*2**((s-t-1))) + w
t=t+1
print(w)
def convertisseur_binaire_en_hexa(o):
o=str(o)
z=len(o)%4
i=0
d=0
rf=''
if(z==1):
o= str(0)+str(0)+str(0)+o
elif(z==2):
o= str(0)+str(0)+o
elif(z==3):
o= str(0)+str(0)+str(0)+o
a = len(o)/4
while (i<a) :
p=o[d:d+4]
if (p=='0000') :
r=0
elif (p=='0001') :
r=1
elif (p=='0010') :
r=2
elif (p=='0011') :
r=3
elif (p=='0100') :
r=4
elif (p=='0101') :
r=5
elif (p=='0110') :
r=6
elif (p=='0111') :
r=7
elif (p=='1000') :
r=8
elif (p=='1001') :
r=9
elif (p=='1010') :
r='A'
elif (p=='1011') :
r='B'
elif (p=='1100') :
r='C'
elif (p=='1101') :
r='D'
elif (p=='1110') :
r='E'
elif (p=='1111') :
r='F'
d=d+4
i=i+1
rf=str(rf)+' '+str(r)
print (rf)
def convertisseur_hexa_en_binaire(x):
j=0
e=0
nf=''
while (j<len(x)) :
q=x[e]
if (q=='0') :
h='0000'
elif (q=='1') :
h='0001'
elif (q=='2') :
h='0010'
elif (q=='3') :
h='0011'
elif (q=='4') :
h='0100'
elif (q=='5') :
h='0101'
elif (q=='6') :
h='0110'
elif (q=='7') :
h='0111'
elif (q=='8') :
h='1000'
elif (q=='9') :
h='1001'
elif (q=='A') :
h='1010'
elif (q=='B') :
h='1011'
elif (q=='C') :
h='1100'
elif (q=='D') :
h='1101'
elif (q=='E') :
h='1110'
elif (q=='F'):
h='1111'
e=e+1
j=j+1
nf=nf+' '+h
print (nf)
def convertisseur_deci_en_hexa(n):
convertisseur_binaire_en_hexa(convertisseur_deci_en_binaire(n))
def convertisseur_hexa_en_deci(x):
convertisseur_binaire_en_deci(convertisseur_hexa_en_binaire(x))
base=float(input("veuillez choisir la base de nombre que vous souhaitez convertir: 2 pour convertir un nombre binaire, 10 pour convertir un nombre décimal et 16 pour convertir un nombre hexadecimal"))
if(base ==2):
nb=str(input("veuillez entrer un nombre binaire"))
print("le nombre binaire ",nb," fait ", convertisseur_binaire_en_deci(nb),"en nombre decimal")
print("le nombre binaire ",nb," fait ", convertisseur_binaire_en_hexa(nb),"en nombre hexadecimal")
elif(base==10):
nb=int(input("veuillez entrer un nombre decimal"))
print("le nombre decimal ",nb," fait ", convertisseur_deci_en_binaire(nb),"en nombre binaire")
print("le nombre decimal ",nb," fait ", convertisseur_deci_en_hexa(nb),"en nombre hexadecimal")
elif(base==16):
nb=str(input("veuillez entrer un nombre hexadecimal"))
print("le nombre hexadecimal ",nb," fait ", convertisseur_hexa_en_binaire(nb),"en nombre binaire")
print("le nombre hexadecimal ",nb," fait ", convertisseur_hexa_en_deci(nb),"en nombre decimal") |
Partager