{"id":129288,"date":"2022-10-28T18:11:35","date_gmt":"2022-10-28T18:11:35","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=845462"},"modified":"2022-10-28T18:11:35","modified_gmt":"2022-10-28T18:11:35","slug":"hex-string-to-hex-integer-in-python","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/10\/28\/hex-string-to-hex-integer-in-python\/","title":{"rendered":"Hex String to Hex Integer in Python"},"content":{"rendered":"\n<div class=\"kk-star-ratings kksr-auto kksr-align-left kksr-valign-top\" data-payload=\"{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;845462&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\\\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}\">\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> 5\/5 &#8211; (1 vote) <\/div>\n<\/div>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"765\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/image-268-1024x765.png\" alt=\"\" class=\"wp-image-845654\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/image-268-1024x765.png 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/image-268-300x224.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/image-268-768x574.png 768w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/image-268.png 1255w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n<p class=\"has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4ac.png\" alt=\"\ud83d\udcac\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Question<\/strong>: Given a hexadecimal string such as <code>'0xf'<\/code> in Python. How to convert it to a hexadecimal number in Python so that you can perform arithmetic operations such as addition and subtraction?<\/p>\n<p>The hexadecimal string representation with the <code>'0x'<\/code> prefix indicates that the digits of the numbers do have a hexadecimal base <code>16<\/code>.<\/p>\n<p>In this article, I&#8217;ll show you how to do some basic conversion and arithmetic computations using the hexadecimal format. So, let&#8217;s get started! <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f447.png\" alt=\"\ud83d\udc47\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<h2>Convert Hex to Decimal using int()<\/h2>\n<p>You can <a href=\"https:\/\/blog.finxter.com\/how-to-convert-hex-string-to-integer-in-python\/\" data-type=\"post\" data-id=\"27255\" target=\"_blank\" rel=\"noreferrer noopener\">convert<\/a> any hexadecimal string to a decimal number using the <code><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-int-function\/\" data-type=\"post\" data-id=\"22715\" target=\"_blank\">int()<\/a><\/code> function with the <code>base=16<\/code> argument. For example, <code>'0xf'<\/code> can be converted to a decimal number using <code>int('0xf', base=16)<\/code> or simply <code>int('0xf', 16)<\/code>. <\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">>>> int('0xf', base=16)\n15\n>>> int('0xf', 16)\n15<\/pre>\n<h2>Hexadecimal Number to Integer Without Quotes<\/h2>\n<p>Note that you can also write the hexadecimal number without the string quotes like so:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">>>> 0xf\n15<\/pre>\n<p>The <code>0x<\/code> prefix already indicates that it is a hexadecimal number.<\/p>\n<h2>Using the eval() Function<\/h2>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"753\" height=\"938\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/image-269.png\" alt=\"\" class=\"wp-image-845664\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/image-269.png 753w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/image-269-241x300.png 241w\" sizes=\"auto, (max-width: 753px) 100vw, 753px\" \/><\/figure>\n<\/div>\n<p>That&#8217;s why an alternative way to convert a hexadecimal string to a numerical value (integer, base 10) is to use the <code>eval('0xf')<\/code> function like so:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">>>> eval('0xf')\n15<\/pre>\n<p>However, I wouldn&#8217;t recommend it over the <code><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-int-function\/\" data-type=\"post\" data-id=\"22715\" target=\"_blank\">int()<\/a><\/code> function as the <code><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-eval\/\" data-type=\"post\" data-id=\"19204\" target=\"_blank\">eval()<\/a><\/code> function is known to be a bit tricky and poses some security risks.<\/p>\n<h2>Hex Arithmetic Operators<\/h2>\n<p>You can simply add or subtract two hexadecimal numbers in Python by using the normal <code>+<\/code> and <code>-<\/code> operators:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">>>> 0xf + 0x1\n16\n>>> 0xf - 0xa\n5\n>>> 0x1 + 0x1\n2<\/pre>\n<p>The result is always shown in decimal values, i.e., with <code>base=10<\/code>.<\/p>\n<p class=\"has-global-color-8-background-color has-background\">You can display the result with <code>base=16<\/code> by converting it back to a hexadecimal format using the <code><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-hex-function\/\" data-type=\"post\" data-id=\"24436\" target=\"_blank\">hex()<\/a><\/code> built-in function. For example, the expression <code>hex(0x1 + 0x1)<\/code> yields the hexadecimal string representation <code>'0x2'<\/code>. <\/p>\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube\"><a href=\"https:\/\/blog.finxter.com\/hex-string-to-hex-integer-in-python\/\"><img decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/plugins\/wp-youtube-lyte\/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FF6e9mlCZWkw%2Fhqdefault.jpg\" alt=\"YouTube Video\"><\/a><figcaption><\/figcaption><\/figure>\n<p>Here are a couple of examples:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">>>> hex(0x1 + 0x1) '0x2'\n>>> hex(0xf + 0xf) '0x1e'\n>>> hex(0xf * 16) '0xf0'<\/pre>\n<p>In the last line, you multiply with the base <code>16<\/code> which essentially shifts the whole number one digit and inserts a <code>0<\/code> digit at the right&#8212;much like multiplying with base <code>10<\/code> in a decimal system.<\/p>\n<h2>Adding Two Hex Strings<\/h2>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"683\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/image-270-1024x683.png\" alt=\"\" class=\"wp-image-845678\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/image-270-1024x683.png 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/image-270-300x200.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/image-270-768x512.png 768w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/image-270.png 1314w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n<p>In the following example, you add together two hex strings <code>'0xf'<\/code> and <code>'0xf'<\/code>&#8212;both representing the decimal 15 so the result is decimal 30:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">>>> int('0xf', 16) + int('0xf', 16)\n30<\/pre>\n<p>If you need the result as a hex string, you can pass the whole computation into the <code><a href=\"https:\/\/blog.finxter.com\/python-hex-function\/\" data-type=\"post\" data-id=\"24436\" target=\"_blank\" rel=\"noreferrer noopener\">hex()<\/a><\/code> built-in function to obtain a hexadecimal representation of the decimal <code>30<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">>>> hex(int('0xf', 16) + int('0xf', 16)) '0x1e'<\/pre>\n<h2>Subtracting and Multiplying Two Hex Strings<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">You can also <a href=\"https:\/\/blog.finxter.com\/python-subtraction-operator\/\" data-type=\"post\" data-id=\"31240\" target=\"_blank\" rel=\"noreferrer noopener\">subtract<\/a> or <a href=\"https:\/\/blog.finxter.com\/python-multiplication-operator\/\" data-type=\"post\" data-id=\"31390\" target=\"_blank\" rel=\"noreferrer noopener\">multiply<\/a> two hex strings by converting them to integers from their base <code>16<\/code> representations using <code>int(hex_str, 16)<\/code>, doing the computation in the decimal system using the normal <code>-<\/code> and <code>*<\/code> operators, and converting back to hexadecimal strings using the <code>hex()<\/code> function on the result.<\/p>\n<p>See here:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">>>> h1 = '0xf'\n>>> h2 = '0x1'\n>>> h1_int = int(h1, 16)\n>>> h2_int = int(h2, 16)\n>>> hex(h1_int - h2_int) '0xe'\n>>> hex(h1_int * h2_int) '0xf'<\/pre>\n<h2>Printing Hex String without Prefix &#8216;0x&#8217;<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">To print the hexadecimal string such as <code>'0xffffff'<\/code> without the <code>'0x'<\/code> prefix, you can simply use <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/daily-python-puzzle-string-slicing\/\" data-type=\"post\" data-id=\"124\" target=\"_blank\">slicing<\/a> <code>hex_string[2:]<\/code> starting from the third character and slice all the way to the right. <\/p>\n<p>A minimal example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">>>> hex_string = '0xfffffff'\n>>> hex_string[2:] 'fffffff'<\/pre>\n<h2>Where to Go From Here?<\/h2>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"683\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/image-271-1024x683.png\" alt=\"\" class=\"wp-image-845687\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/image-271-1024x683.png 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/image-271-300x200.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/image-271-768x512.png 768w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/image-271.png 1314w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n<p>Thanks for reading through the whole article, I&#8217;d love to see you around more often in the Finxter community to learn and improve your coding skills. <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/2764.png\" alt=\"\u2764\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<p>If you also want to learn, join our <a href=\"https:\/\/blog.finxter.com\/email-academy\/\" data-type=\"page\" data-id=\"12278\" target=\"_blank\" rel=\"noreferrer noopener\">free email academy<\/a> and download our cheat sheets here:<\/p>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) Question: Given a hexadecimal string such as &#8216;0xf&#8217; in Python. How to convert it to a hexadecimal number in Python so that you can perform arithmetic operations such as addition and subtraction? The hexadecimal string representation with the &#8216;0x&#8217; prefix indicates that the digits of the numbers do have a hexadecimal [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-129288","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/129288","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=129288"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/129288\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=129288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=129288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=129288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}