How to Convert Decimal to Binary in Python - NareshIT
Naresh i Technologies
Only Institute to offer the 'Most Comprehensive eLearning Platform to suit the self-learning needs of all CMS and LMS
Introduction:
1. Python is considered a highly versatile and sophisticated programming language which is having enriched library set with itself.
2.?????It is capable to interpret any complex programming and ability to solve it.
3.?????Among the many problems if we are trying to convert from decimal to binary and vice versa then python has also provided that feature.
4.?????When we are trying to convert the decimal to a Binary value then we can make the use of bin ()function.
5.?????Similarly, the vice versa is also can be done using the inbuilt function int().
Here, I am going to discuss How to convert the decimal value to a binary value and vice versa in Python. Here we are also going to discuss how they are used to support the inbuilt functions which are being provided by the Python library.
Basic Procedure for Converting the Decimal value to Binary value:
In standard ways when we are going to convert the decimal to binary value then we used to follow the following steps as mentioned below.
1.?????Keep calling the conversion function with n/2 till n > 1,
2.?????later perform n % 1 to get the MSB of a converted binary number.??
Let us consider that we are going to convert the decimal value of 7 to a binary value. To do so, as I have already mentioned the steps above, we need to proceed as
1.?????7/2 = Quotient = 3(grater than 1), Remainder = 1. So, repeat step 1 again.
2.?????3/2 = Quotient = 1(not grater than 1), Remainder = 1.So, repeat step 1 again.
3.?????1%2 = Rem = 1. Here we need to stop, and we need to reverse count the value as (111) 2.
Implementation of program in Python:
In Python, we can convert from decimal to binary in three ways. I am going to discuss all three approaches below.
Approach 1: We can use the recursive technique here. To do so we need to proceed as follows.
DecimalToBinary(num):
??????? if num>= 1:
DecimalToBinary(num // 2)
??????????? print num % 2
To implement the code in Python we can proceed as follows.
# Function to convert decimal numbers binary using recursion technique.
def DecimalToBinary(num):
???? ?if num>= 1:
????????DecimalToBinary(num // 2)
????print(num % 2, end = '')
?# Driver Code
if __name__ == '__main__':
?????# decimal value
????dec_val = 24
?????# Calling function
????DecimalToBinary(dec_val)
Approach-2: Decimal to binary using in-built function.
Here we can make the use of bin()function. The implementation can be done as follows.
# use of the binary function to convert Decimal numbers to Binary numbers
?def decimaltobinary(n):
????return bin(n).replace("0b", "")
???# Driver code
if __name__ == '__main__':
????print(decimaltobinary(8))
output: 1000.
Approach-3: Without the use of an inbuilt function also we can do the conversion. If we need to do so, then we need to proceed as follows.
def decimalToBinary(n):
????return "{0:b}".format(int(n))
??# Driver code
if __name__ == '__main__':
????print(decimalToBinary(8))
?Output: 1000
领英推荐
Binary to Decimal in Python:
In ordinary ways when we are going to convert the binary to decimal value then we used to follow the following steps as mentioned below. Let us discuss this here by considering an example. Let us consider that we are going to find the decimal equivalent of a binary value (1011)2. I am going to discuss the steps below.
1.?????First we need to take the modulo of the given binary number(1011)2 with 10.?
(1011 % 10 = 1) it is the modulo division.
2.?????After getting this now we need to multiply the reminder value by 2 and raised the power.
It’s positioned from the right end.?
(1?????* 2^0) It should be get noted that we are starting the counting position with 0.?
3.?????Add the obtained result value with the previously generated result.
?decimal = decimal + (1 * 2^0)
4.??????Update the binary number by dividing it by 10.
(10110 = 101)
5.?????Keep repeating the above steps until the given binary number value is> 0.
Final Conversion -: (1 * 2^3) + (0 * 2^2) +(1 * 2^1) + (1 * 2^0) = 11
So, from the final conversion, we came to know that the decimal equivalent of (1011) 2 is 11.
Implementation of program in Python:
In Python, we can do the conversion from binary to decimal in the following two manners. Here I am going to discuss both approaches below.
Program:
Approach-1: Without using the inbuilt function. Here we can use the loop concept to make the conversion.
Program:
def binaryToDecimal(binary):
??????binary1 = binary
????decimal, i, n = 0, 0, 0
????while(binary != 0):
????????dec = binary % 10
????????decimal = decimal + dec * pow(2, i)
????????binary = binary//10
????????i += 1
????print(decimal)????
???# Driver code
if __name__ == '__main__':
????binaryToDecimal(100)
?Output: 4.
Approach-2: Using the inbuilt functionint().
We can also able to do the program using the inbuilt function int(). To do so, we need to proceed as follows.
Program:
def binaryToDecimal(n):
????return int(n,2)
# Driver code
if __name__ == '__main__':
????print(binaryToDecimal('100'))
Output: 4.
Scope @ NareshIT:
1.?????At Naresh IT you will get a good Experienced faculty who will guide you, mentor you and nurture you to achieve your dream goal.
2.?????Here you will get good hand on practice in terms of the practical industry-oriented environment which will definitely help you a lot to shape your future.
3.?????During the designing process of the application, we will let you know about the other aspect of the application.
4.?????Our Expert trainer will let you know about every ins and out’s of the problem scenario.
Achieving your dream goal is our motto. Our excellent team is working restlessly for our students to click their target. So, believe in us and our advice, and we assured you about your sure success.?
You can contact us anytime for your?Python Training?online as well as call us directly, or you can give us a missed call. And one of our customer care representatives will contact you asap.
Follow us for More Updates:?https://bit.ly/NITLinkedIN??