-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertToBin.py
More file actions
51 lines (37 loc) · 1.56 KB
/
ConvertToBin.py
File metadata and controls
51 lines (37 loc) · 1.56 KB
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
# en cas de hex, convertir en dec, puis en bin
# en cas de dec, convertir en bin directement
# importation fonc de Marc Antoine, pour convertir tout n \
# ayant une base définit en binaire, necessaire pour convertir de hex en bin
from ConvertToDec import convert_to_dec
# renverser la chaîne de caractères
def reverse_str (t):
return t [::-1]
# convertir du decimal au binaire, renversement du résultat pour l'afficher correctement
def convert_dec_to_bin (n):
converted_n = ""
while True:
result, remainder = divmod (int (n), 2)
converted_n += str (remainder)
if result == 0:
break
n = result
converted_n = reverse_str (converted_n)
return converted_n
# utilisation de la fonction convert_to_dec, obtention du résultat en dec, puis conversion en bin, via la fonction édictée plus haut
def convert_hex_to_bin (h):
n = int (convert_to_dec (h, "hex"))
return convert_dec_to_bin (n)
# choix des fonctions utilisées pour convertir 'n' en base binaire
def convert_to_bin (n, base):
if base == "dec":
return convert_dec_to_bin(n)
elif base == "hex":
return convert_hex_to_bin(n)
elif base == "bin":
return n
else:
return "vous n'avez pas entrer une base correcte, veuillez rééssayer avec une hex, bin ou dec"
if __name__ == "__main__":
print(convert_to_bin("-1988971", "dec") == "-111100101100101101011")
print(convert_to_bin("1ae867f987d", "hex") == "11010111010000110011111111001100001111101")
print(convert_to_bin("100100101", "bin") == "100100101")