Sick Gaming
[Tut] Hex String to Hex Integer in Python - Printable Version

+- Sick Gaming (https://www.sickgaming.net)
+-- Forum: Programming (https://www.sickgaming.net/forum-76.html)
+--- Forum: Python (https://www.sickgaming.net/forum-83.html)
+--- Thread: [Tut] Hex String to Hex Integer in Python (/thread-100161.html)



[Tut] Hex String to Hex Integer in Python - xSicKxBot - 10-30-2022

Hex String to Hex Integer in Python

<div>
<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;}">
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 142.5px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div>
</div>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" 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="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4ac.png" alt="?" 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>
<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>
<p>In this article, I’ll show you how to do some basic conversion and arithmetic computations using the hexadecimal format. So, let’s get started! <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f447.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<h2>Convert Hex to Decimal using int()</h2>
<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>
<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)
15
>>> int('0xf', 16)
15</pre>
<h2>Hexadecimal Number to Integer Without Quotes</h2>
<p>Note that you can also write the hexadecimal number without the string quotes like so:</p>
<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
15</pre>
<p>The <code>0x</code> prefix already indicates that it is a hexadecimal number.</p>
<h2>Using the eval() Function</h2>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img 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="(max-width: 753px) 100vw, 753px" /></figure>
</div>
<p>That’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>
<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')
15</pre>
<p>However, I wouldn’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>
<h2>Hex Arithmetic Operators</h2>
<p>You can simply add or subtract two hexadecimal numbers in Python by using the normal <code>+</code> and <code>-</code> operators:</p>
<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
16
>>> 0xf - 0xa
5
>>> 0x1 + 0x1
2</pre>
<p>The result is always shown in decimal values, i.e., with <code>base=10</code>.</p>
<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>
<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 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>
<p>Here are a couple of examples:</p>
<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'
>>> hex(0xf + 0xf) '0x1e'
>>> hex(0xf * 16) '0xf0'</pre>
<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—much like multiplying with base <code>10</code> in a decimal system.</p>
<h2>Adding Two Hex Strings</h2>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img 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="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<p>In the following example, you add together two hex strings <code>'0xf'</code> and <code>'0xf'</code>—both representing the decimal 15 so the result is decimal 30:</p>
<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)
30</pre>
<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>
<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>
<h2>Subtracting and Multiplying Two Hex Strings</h2>
<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>
<p>See here:</p>
<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'
>>> h2 = '0x1'
>>> h1_int = int(h1, 16)
>>> h2_int = int(h2, 16)
>>> hex(h1_int - h2_int) '0xe'
>>> hex(h1_int * h2_int) '0xf'</pre>
<h2>Printing Hex String without Prefix ‘0x’</h2>
<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>
<p>A minimal example:</p>
<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'
>>> hex_string[2:] 'fffffff'</pre>
<h2>Where to Go From Here?</h2>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img 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="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<p>Thanks for reading through the whole article, I’d love to see you around more often in the Finxter community to learn and improve your coding skills. <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/2764.png" alt="❤" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<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>
</div>


https://www.sickgaming.net/blog/2022/10/28/hex-string-to-hex-integer-in-python/