hex()

hex(number)
number :input integer number
Return the hexal number as string.

Examples

my_num=27
print(hex(my_num))  # 0x1b

Using Float

This will generate TypeError
num=27.45
print(hex(num))
TypeError: 'float' object cannot be interpreted as an integer

Binary to hex

print(hex(0b11011)) # 0x1b
In above code the input binary number is equivalent to integer 27.

You can use bin() to get the binary output
print("Binary Number : ", bin(27)) # Binary Number :  0b11011

octal to hex

print(hex(0o33)) # 0x1b
In above code the input Octal number is equivalent to integer 27 You can use oct() to get the Octal output
print("Octal  Number : ", oct(27)) # Octal  Number :  0o33
In our hex output, 0x is always prefixed. As the output is a string we can remove it like this
my_hex=hex(27)
print(my_hex[2:]) # 1b
Without using hex() and by using format()
my_num=27
print(format(my_num,'x')) # 1b
All Built in Functions in Python
max() Bitwise operators int() oct() float()


Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2023 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer