We all know how to convert a number from decimal to octal, binary, and hexadecimal, as we would have solved many mathematical problems based on them. Now, you will learn how to do these conversions using the Python programming language.
Decimal to Binary
First, we will see how to convert a number from decimal to binary. We can convert a number from decimal to binary using different methods, which are:
Using Recursion
def DecimalToBinary(number):
if num >= 1:
DecimalToBinary(number // 2)
print(number % 2, end = '')
# Driver Code
if __name__ == '__main__':
# decimal value
decimal_val = 17
# Calling function
DecimalToBinary(decimal_val)
Output:
10001
Using In-Built Function
def decimalToBinary(num):
return bin(num).replace("0b", "")
# Driver code
if __name__ == '__main__':
print(decimalToBinary(10))
print(decimalToBinary(24))
print(decimalToBinary(9))
Output:
1010
11000
1001
Without Built-in function
def decimalToBinary(num):
return "{0:b}".format(int(num))
# Driver code
if __name__ == '__main__':
print(decimalToBinary(10))
print(decimalToBinary(24))
print(decimalToBinary(9))
Output:
1010
11000
1001
Decimal to Octal
Now, let us see how we can convert a number from decimal to octal using the Python programming language. First let us write down the algorithm before solving the problem, which helps us write the code easily.
Algorithm
- After dividing the number by 8, we store the remainder in an array
- Divide the number by 8 now
- Repeat the above two steps until the number becomes equal to zero
- Now, print the array in reverse order
Python_Code
def decToOctal(num):
# array to store
# octal number
octalNumber = [0] * 100
# counter for octal
# number array
i = 0
while (num!= 0):
# storing remainder
# in octal array
octalNumber[i] = num % 8
num = int(num / 8)
i += 1
# printing octal number
# array in reverse order
for k in range(i - 1, -1, -1):
print(octalNumber[k], end="")
# Driver Code
num = 33
# Function Call
decToOctal(num)
Output:
41
Decimal to Hexadecimal
Now. Let us see how we can convert a number from decimal to hexadecimal using the Python programming language.
Algorithm
- After dividing the number by 16, store the remainder in a temporary variable temp1.
- If the temp1<10, insert( 48+temp1) into the array otherwise insert (55+temp1) into the array.
- Now, divide the number by 16
- Repeat the above steps until the number becomes equal to zero
- Now, print the array in reverse order
Code:
def decToHexa(num):
# char array to store
# hexadecimal number
hexaDeciNumber = ['0'] * 100
# counter for hexadecimal
# number array
I = 0
while(num!= 0):
# temporary variable
# to store remainder
temp = 0
# storing remainder
# in temp variable.
temp = num % 16
# check if temp < 10
if(temp < 10):
hexaDeciNumber[i] = chr(temp + 48)
i = i + 1
else:
hexaDeciNumber[i] = chr(temp + 55)
i = i + 1
num = int(num / 16)
# printing hexadecimal number
# array in reverse order
j = i - 1
while(j >= 0):
print((hexaDeciNumber[j]), end="")
j = j - 1
# Driver Code
num = 2545
decToHexa(num)
Output:
41
Q&A on Decimal to Octal
Now, to give you a quick brief on how these conversions work, we have answered some frequently answered questions for you, which are:
Q1. How do you convert from decimal to octal?
A. We can convert from octal to decimal by following these steps:
def octalToDecimal(num):
number = n
dec_value = 0
# Initializing base value
# to 1, i.e 8^0
base = 1
temp = number
while (temp):
# Extracting last digit
last_digit = temp % 10
temp = int(temp / 10)
# Multiplying last digit
# with appropriate base
# value and adding it
# to dec_value
dec_value += last_digit * base
base = base * 8
return dec_value
# Driver Code
number = 67
print(octalToDecimal(number))
- After dividing the number by 8, we store the remainder in an array
- Divide the number by 8 now
- Repeat the above two steps until the number becomes equal to zero
- Now, print the array in reverse order
We can convert decimal to hexadecimal by following these steps:
- After dividing the number by 16, store the remainder in a temporary variable temp1.
- If the temp1<10, insert( 48+temp1) into the array otherwise insert (55+temp1) into the array.
- Now, divide the number by 16
- Repeat the above steps until the number becomes equal to zero
- Now, print the array in reverse order
Leave a Reply