1
1
# Python: Binary to Decimal Conversion
2
2
# binToDec and decToBin functions are rendered obsolete by the universal convert function
3
3
4
- def binToDec (binNum ):
4
+ def binToDec (binNum ): #function created to convert binary to decimal with parametere binNum
5
5
decNum = 0
6
6
power = 0
7
- while binNum > 0 :
8
- decNum += 2 ** power * (binNum % 10 )
9
- binNum //= 10
10
- power += 1
7
+ while binNum > 0 : #loop will run till binNum is greater than 0
8
+ decNum += 2 ** power * (binNum % 10 )
9
+ binNum //= 10 # reducing binNum everytime by 1 digit
10
+ power += 1 # increasing power by 1 each loop
11
11
return decNum
12
12
13
- def decToBin (decNum ):
13
+ def decToBin (decNum ): #function created to convert decimal to binary with parametere decNum
14
14
binNum = 0
15
15
power = 0
16
- while decNum > 0 :
16
+ while decNum > 0 :#loop will run till decNum is greater than 0
17
17
binNum += 10 ** power * (decNum % 2 )
18
- decNum //= 2
19
- power += 1
18
+ decNum //= 2 # reducing decNum everytime by 1 digit
19
+ power += 1 # increasing power by 1 each loop
20
20
return binNum
21
21
22
- def convert (fromNum , fromBase , toBase ):
22
+ def convert (fromNum , fromBase , toBase ): #function for converting from any base to any other base
23
23
toNum = 0
24
24
power = 0
25
25
while fromNum > 0 :
@@ -31,4 +31,4 @@ def convert(fromNum, fromBase, toBase):
31
31
# print (str(binToDec(101011)))
32
32
# print (str(decToBin(128)))
33
33
print (str (convert (127 , 10 , 8 ))) # converts 127 in base 10 to base 8
34
- print (str (convert (101001 , 2 , 2 )))
34
+ print (str (convert (101001 , 2 , 2 )))
0 commit comments