hex() Function in Python Programming Language
hex() Function
hex() Function in Python Programming Language is used to convert any type of representation of a number to hexadecimal. Hexadecimal numbers are represented with base 10. Representation of these numbers is done in a unique way it used numbers from 0 to 9 to represent the first 10 numbers and then uses A to F alphabets to represent numbers from 10 to 15.
Hexadecimal Representation
Hexadecimal Representation of a number is done using numbers from 0-9 and alphabets from A-F. Any representation of the number can be converted into hexadecimal by using hex() function. hex() function is an in-built function of python library. hex() function takes only one argument. As a representation of the number is passed in the function it returns the hexadecimal representation of the argument. Let’s have a look at python code to convert some number representation in hexadecimal format.
Python Program to convert numbers to Hexadecimal Format
#integer number int_number = 14956 #converting number to hexadecimal #using hex function Hexa_number = hex(int_number) print(str(Hexa_number) + ' is hexadecimal representation of number with base 10') #binary number bin_number = 0b101010 #converting binary number to hexadecimal #using hex function Hexa_number = hex(bin_number) print(str(Hexa_number) + ' is hexadecimal representation of number with base 2') #octal number oct_number = 0o15437 #converting octal number to hexadecimal #using hex function Hex_number = hex(oct_number) print(str(Hexa_number) + ' is hexadecimal representation of number with base 8')
Output: 0x3a6c is hexadecimal representation of number with base 10 0x2a is hexadecimal representation of number with base 10 0x1b1f is hexadecimal representation of number with base 10
In the above python program where we converted numbers with base 10, 2 and 8 in the hexadecimal format or number with base 16 we used the in-built hex() function. As the number enters the function it internally converts the number into the hexadecimal format and then using the print function we print it on the screen.
FAQ's
How do you represent a hex in python?
To represent a hexadecimal value we use 0x in the prefix of the number. 0x before the number represents the number to be in a hexadecimal format. In python, we can convert any number in hexadecimal by using hex() function.
What is octal value in Python?
Number with base 8 is referred to as the octal number. When a number is represented with the prefix 0o then the number is considered as an octal number
How do you print a binary value in Python?
Binary numbers are numbers with base 2. These type of numbers can be represented with the prefix 0b. Any number can be converted to binary representation using bin() function.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment