Python hex() | DigitalOcean (2024)

Tutorial

Published on August 3, 2022

Python hex() | DigitalOcean (1)

By Pankaj

Python hex() | DigitalOcean (2)

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Python hex() function is used to convert an integer to a lowercase hexadecimal string prefixed with “0x”. We can also pass an object to hex() function, in that case the object must have __index__() function defined that returns integer. The input integer argument can be in any base such as binary, octal etc. Python will take care of converting them to hexadecimal format.

Python hex() example

Let’s look into some simple examples of converting integer to hexadecimal number.

print(hex(255)) # decimalprint(hex(0b111)) # binaryprint(hex(0o77)) # octalprint(hex(0XFF)) # hexadecimal

Output:

0xff0x70x3f0xff

Python hex() with object

Let’s create a custom class and define __index__() function so that we can use hex() function with it.

class Data: id = 0 def __index__(self): print('__index__ function called') return self.idd = Data()d.id = 100print(hex(d))

Output:

__index__ function called0x64

You can checkout complete python script and more Python examples from our GitHub Repository.

Reference: Official Documentation

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us

About the authors

Python hex() | DigitalOcean (3)

Pankaj

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk

Python hex() | DigitalOcean (5)

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Python hex() | DigitalOcean (6)

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Python hex() | DigitalOcean (7)

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more

Python hex() | DigitalOcean (8)

© 2024 DigitalOcean, LLC.Sitemap.

Python hex()  | DigitalOcean (2024)
Top Articles
Latest Posts
Article information

Author: The Hon. Margery Christiansen

Last Updated:

Views: 6243

Rating: 5 / 5 (50 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: The Hon. Margery Christiansen

Birthday: 2000-07-07

Address: 5050 Breitenberg Knoll, New Robert, MI 45409

Phone: +2556892639372

Job: Investor Mining Engineer

Hobby: Sketching, Cosplaying, Glassblowing, Genealogy, Crocheting, Archery, Skateboarding

Introduction: My name is The Hon. Margery Christiansen, I am a bright, adorable, precious, inexpensive, gorgeous, comfortable, happy person who loves writing and wants to share my knowledge and understanding with you.