{"id":5548,"date":"2020-04-30T13:35:53","date_gmt":"2020-04-30T13:35:53","guid":{"rendered":"https:\/\/www.askpython.com\/?p=5548"},"modified":"2022-08-06T13:28:41","modified_gmt":"2022-08-06T13:28:41","slug":"python-hex-function","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/built-in-methods\/python-hex-function","title":{"rendered":"How to use the Python hex() function?"},"content":{"rendered":"\n<p>In this article, we&#8217;ll cover the Python <strong>hex()<\/strong> function.<\/p>\n\n\n\n<p>This function is useful if you want to convert an Integer to a hexadecimal <a href=\"https:\/\/www.askpython.com\/python\/string\/python-string-functions\" class=\"rank-math-link\">string<\/a>, prefixed with <strong>&#8220;0x&#8221;<\/strong>.<\/p>\n\n\n\n<p>Let&#8217;s look at how we could use this function.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Using the Python hex() function<\/h2>\n\n\n\n<p>The Python hex() function has a very simple syntax:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nhex_string = hex(val)\n<\/pre><\/div>\n\n\n<p>Here, <code>val<\/code> can be an Integer, binary, octal, or a hexadecimal number.<\/p>\n\n\n\n<p>Let&#8217;s look at some examples quickly.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(hex(1000))  # decimal\nprint(hex(0b111))  # binary\nprint(hex(0o77))  # octal\nprint(hex(0XFF))  # hexadecimal\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n0x3e8\n0x7\n0x3f\n0xff\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Using Python hex() on a custom object<\/h2>\n\n\n\n<p>We can also use hex() on a custom object. But, if we want to use it successfully, we must define the __index__() dunder method for our class.<\/p>\n\n\n\n<p>The hex() method will call <code>__index__()<\/code>, so we must implement it. This must return a value, which can be a decimal \/ binary \/ octal \/ hexadecimal number.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass MyClass:\n    def __init__(self, value):\n        self.value = value\n    def __index__(self):\n        print(&#039;__index__() dunder method called&#039;)\n        return self.value\n\n\nmy_obj = MyClass(255)\n\nprint(hex(my_obj))\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n__index__() dunder method called\n0xff\n<\/pre><\/div>\n\n\n<p>Indeed, as you can see, it returns what we expected.<\/p>\n\n\n\n<p>First, <code>hex()<\/code> calls the __index__ method on our custom class.<\/p>\n\n\n\n<p>Then, it converts the returned value to a hexadecimal string (<strong>255 -&gt; &#8220;0xff&#8221;<\/strong>)<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this article, we learned about using the <strong>hex() <\/strong>function, to convert numerical values into a hexadecimal string.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>JournalDev article on Python hex()<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we&#8217;ll cover the Python hex() function. This function is useful if you want to convert an Integer to a hexadecimal string, prefixed with &#8220;0x&#8221;. Let&#8217;s look at how we could use this function. Using the Python hex() function The Python hex() function has a very simple syntax: Here, val can be an [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":5552,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-5548","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-built-in-methods"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/5548","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=5548"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/5548\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/5552"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=5548"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=5548"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=5548"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}