Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

Python: Convert decimal to hexadecimal

Python Basic: Exercise-141 with Solution

Write a python program to convert decimal to hexadecimal.

Sample decimal number: 30, 4
Expected output: 1e, 04

Pictorial Presentation:

Convert decimal to hexadecimal

Sample Solution-1:

Python Code:

x = 30
print(format(x, '02x'))
x = 4
print(format(x, '02x'))

Sample Output:

1e                                                                                                            
04 

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:


Sample Solution-2:

Python Code:

def dechimal_to_Hex(n):   
   x = (n % 16)
   ch = ""
   if (x < 10):
       ch = x
   if (x == 10):
       ch = "A"
   if (x == 11):
       ch = "B"
   if (x == 12):
       ch = "ch"
   if (x == 13):
       ch = "D"
   if (x == 14):
       ch = "E"
   if (x == 15):
       ch = "F"
   if (n - x != 0):
       return dechimal_to_Hex(n // 16) + str(ch)
   else:
       return str(ch)
dechimal_nums = [0, 15, 30, 55, 355, 656, 896, 1125]
print("Dechimal numbers:")
print(dechimal_nums)
print("\nHexadechimal numbers of the said dechimal numbers:")
print([dechimal_to_Hex(x) for x in dechimal_nums])

Sample Output:

Dechimal numbers:
[0, 15, 30, 55, 355, 656, 896, 1125]

Hexadechimal numbers of the said dechimal numbers:
['0', 'F', '1E', '37', '163', '290', '380', '465']

Flowchart:

Flowchart: Convert decimal to hexadecimal.

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:


Sample Solution-3:

Python Code:

def dechimal_to_Hex(dechimal_nums):
   digits = "0123456789ABCDEF"
   x = (dechimal_nums % 16)
   rest_part = dechimal_nums // 16
   if (rest_part == 0):
       return digits[x]
   return dechimal_to_Hex(rest_part) + digits[x]
dechimal_nums = [0, 15, 30, 55, 355, 656, 896, 1125]
print("Dechimal numbers:")
print(dechimal_nums)
print("\nHexadechimal numbers of the said dechimal numbers:")
print([dechimal_to_Hex(x) for x in dechimal_nums])

Sample Output:

Dechimal numbers:
[0, 15, 30, 55, 355, 656, 896, 1125]

Hexadechimal numbers of the said dechimal numbers:
['0', 'F', '1E', '37', '163', '290', '380', '465']

Flowchart:

Flowchart: Convert decimal to hexadecimal.

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:


Python Code Editor:

 

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to convert an integer to binary keep leading zeros.
Next: Write a Python program to check if every consecutive sequence of zeroes is followed by a consecutive sequence of ones of same length in a given string. Return True/False.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Python: Tips of the Day

Find current directory and file's directory:

To get the full path to the directory a Python file is contained in, write this in that file:

import os 
dir_path = os.path.dirname(os.path.realpath(__file__))

(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)

To get the current working directory use

import os
cwd = os.getcwd()

Documentation references for the modules, constants and functions used above:

  • The os and os.path modules.
  • The __file__ constant
  • os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
  • os.path.dirname(path) (returns "the directory name of pathname path")
  • os.getcwd() (returns "a string representing the current working directory")
  • os.chdir(path) ("change the current working directory to path")

Ref: https://bit.ly/3fy0R6m