In the previous article, we have discussed Python Program to Convert Decimal to Hexadecimal
Given an octal number and the task is to get the respective hexadecimal number.
Examples:
Example1:
Input:
Given Octal number = 24
Output:
The Hexadecimal value of the given octal number { 24 } is: 14
Example2:
Input:
Given Octal number = 72
Output:
The Hexadecimal value of the given octal number { 72 } is: 3A
Program to Convert Octal to Hexadecimal in Python
Below are the ways to convert the given octal number into hexadecimal :
Method #1: Using While Loop (Static Input)
Approach:
- Give the octal number as static input and store it in a variable.
- Take a variable say ‘k‘ and initialize its value with 0.
- Take a list of length 50 and initialize all the values to zero using the multiplication operator.
- Store it in another variable.
- Take another variable say ‘deciml_num‘ and initialize its value with 0.
- Take another variable say ‘tempry‘ and initialize its value with 0.
- Loop till the given octal number is not equal to zero using the while loop.
- Inside the loop, calculate the value of the given octal number modulus 10 and store it in another variable ‘a’.
- Multiply ‘a’ with the 8 raised to the power tempry using the pow() function and store it in another variable ‘b’.
- Add ‘b’ with the deciml_num and store it in the same variable deciml_num.
- Increment the value of tempry by 1 and store it in the same variable tempry.
- Divide the given octal number by 10 and store it in the same variable gvn_octl_num.
- Loop till the decimal number is not equal to zero using the while loop.
- Calculate the value of deciml_num modulus 16 to get the remainder and store it in a variable.
- Check if the above remainder value is less than 10 using the if conditional statement.
- If the statement is true, then get the character with the ASCII value remainder+48 using the if conditional statement and store it in a variable
- Initialize the hexadecimal list at the index ‘k’ with the above-calculated character.
- Increment the value of k by 1 and store it in the same variable k.
- Else if the statement is false, get the character with the ASCII value remainder+55.
- Store it in another variable.
- Initialize the hexadecimal list at the index ‘k’ with the above-calculated character.
- Increment the value of k by 1 and store it in the same variable k.
- Divide the decimal number by 16 and store it in the same variable deciml_num.
- Loop in decreasing order from k-1 to 0 with the stepsize of -1 using the for loop.
- Print the element present at the iterator of the hexadecimal list to get the hexadecimal value of the given octal number.
- The Exit of the Program.
Below is the implementation:
# Give the octal number as static input and store it in a variable. gvn_octl_num = 24 # Take a variable say 'k' and initialize its value with 0. k = 0 # Take a list of length 50 and initialize all the values to zero using the multiplication # operator. # Store it in another variable. Hexa_deciml = ['0']*50 # Take another variable say 'deciml_num' and initialize its value with 0. deciml_num = 0 # Take another variable say 'tempry' and initialize its value with 0. tempry = 0 print( "The Hexadecimal value of the given octal number {", gvn_octl_num, "} is: ") # Loop till the given octal number is not equal to zero using the while loop. while gvn_octl_num != 0: # Inside the loop, calculate the value of the given octal number modulus 10 and store # it in another variable 'a'. a = (gvn_octl_num % 10) # Multiply 'a' with the 8 raised to the power tempry using the pow() function and store # it in another variable 'b'. b = pow(8, tempry)*a # Add 'b' with the deciml_num and store it in the same variable deciml_num. deciml_num = deciml_num+b # Increment the value of tempry by 1 and store it in the same variable tempry. tempry += 1 # Divide the given octal number by 10 and store it in the same variable gvn_octl_num. gvn_octl_num = gvn_octl_num // 10 # Loop till the decimal number is not equal to zero using the while loop. while deciml_num != 0: # Calculate the value of deciml_num modulus 16 to get the remainder and store it in a # variable. remindr = deciml_num % 16 # Check if the above remainder value is less than 10 using the if conditional statement. if remindr < 10: # If the statement is true, then get the character with the ASCII value remainder+48 # using the if conditional statement and store it in a variable chrvalue = chr(remindr+48) # Initialize the hexadecimal list at the index 'k' with the above-calculated character. Hexa_deciml[k] = chrvalue # Increment the value of k by 1 and store it in the same variable k. k += 1 else: # Else if the statement is false, get the character with the ASCII value remainder+55. # Store it in another variable. chrvalue = chr(remindr+55) # Initialize the hexadecimal list at the index 'k' with the above-calculated character. Hexa_deciml[k] = chrvalue # Increment the value of k by 1 and store it in the same variable k. k += 1 # Divide the decimal number by 16 and store it in the same variable deciml_num. deciml_num //= 16 # Loop in decreasing order from k-1 to 0 with the stepsize of -1 using the for loop. for itr in range(k-1, -1, -1): # Print the element present at the iterator of the hexadecimal list to get the # hexadecimal value of the given octal number. print(Hexa_deciml[itr], end='')
Output:
The Hexadecimal value of the given octal number { 24 } is: 14
Method #2: Using While loop (User Input)
Approach:
- Give the octal number as user input using the int(input()) function input and store it in a variable.
- Take a variable say ‘k‘ and initialize its value with 0.
- Take a list of length 50 and initialize all the values to zero using the multiplication operator.
- Store it in another variable.
- Take another variable say ‘deciml_num‘ and initialize its value with 0.
- Take another variable say ‘tempry‘ and initialize its value with 0.
- Loop till the given octal number is not equal to zero using the while loop.
- Inside the loop, calculate the value of the given octal number modulus 10 and store it in another variable ‘a’.
- Multiply ‘a’ with the 8 raised to the power tempry using the pow() function and store it in another variable ‘b’.
- Add ‘b’ with the deciml_num and store it in the same variable deciml_num.
- Increment the value of tempry by 1 and store it in the same variable tempry.
- Divide the given octal number by 10 and store it in the same variable gvn_octl_num.
- Loop till the decimal number is not equal to zero using the while loop.
- Calculate the value of deciml_num modulus 16 to get the remainder and store it in a variable.
- Check if the above remainder value is less than 10 using the if conditional statement.
- If the statement is true, then get the character with the ASCII value remainder+48 using the if conditional statement and store it in a variable
- Initialize the hexadecimal list at the index ‘k’ with the above-calculated character.
- Increment the value of k by 1 and store it in the same variable k.
- Else if the statement is false, get the character with the ASCII value remainder+55.
- Store it in another variable.
- Initialize the hexadecimal list at the index ‘k’ with the above-calculated character.
- Increment the value of k by 1 and store it in the same variable k.
- Divide the decimal number by 16 and store it in the same variable deciml_num.
- Loop in decreasing order from k-1 to 0 with the stepsize of -1 using the for loop.
- Print the element present at the iterator of the hexadecimal list to get the hexadecimal value of the given octal number.
- The Exit of the Program.
Below is the implementation:
# Give the octal number as user input using the int(input()) function input and # store it in a variable. gvn_octl_num = int(input("Enter some random number = ")) # Take a variable say 'k' and initialize its value with 0. k = 0 # Take a list of length 50 and initialize all the values to zero using the multiplication # operator. # Store it in another variable. Hexa_deciml = ['0']*50 # Take another variable say 'deciml_num' and initialize its value with 0. deciml_num = 0 # Take another variable say 'tempry' and initialize its value with 0. tempry = 0 print( "The Hexadecimal value of the given octal number {", gvn_octl_num, "} is: ") # Loop till the given octal number is not equal to zero using the while loop. while gvn_octl_num != 0: # Inside the loop, calculate the value of the given octal number modulus 10 and store # it in another variable 'a'. a = (gvn_octl_num % 10) # Multiply 'a' with the 8 raised to the power tempry using the pow() function and store # it in another variable 'b'. b = pow(8, tempry)*a # Add 'b' with the deciml_num and store it in the same variable deciml_num. deciml_num = deciml_num+b # Increment the value of tempry by 1 and store it in the same variable tempry. tempry += 1 # Divide the given octal number by 10 and store it in the same variable gvn_octl_num. gvn_octl_num = gvn_octl_num // 10 # Loop till the decimal number is not equal to zero using the while loop. while deciml_num != 0: # Calculate the value of deciml_num modulus 16 to get the remainder and store it in a # variable. remindr = deciml_num % 16 # Check if the above remainder value is less than 10 using the if conditional statement. if remindr < 10: # If the statement is true, then get the character with the ASCII value remainder+48 # using the if conditional statement and store it in a variable chrvalue = chr(remindr+48) # Initialize the hexadecimal list at the index 'k' with the above-calculated character. Hexa_deciml[k] = chrvalue # Increment the value of k by 1 and store it in the same variable k. k += 1 else: # Else if the statement is false, get the character with the ASCII value remainder+55. # Store it in another variable. chrvalue = chr(remindr+55) # Initialize the hexadecimal list at the index 'k' with the above-calculated character. Hexa_deciml[k] = chrvalue # Increment the value of k by 1 and store it in the same variable k. k += 1 # Divide the decimal number by 16 and store it in the same variable deciml_num. deciml_num //= 16 # Loop in decreasing order from k-1 to 0 with the stepsize of -1 using the for loop. for itr in range(k-1, -1, -1): # Print the element present at the iterator of the hexadecimal list to get the # hexadecimal value of the given octal number. print(Hexa_deciml[itr], end='')
Output:
Enter some random number = 72 The Hexadecimal value of the given octal number { 72 } is: 3A
Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.