Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Easiest Way to Convert List of Hex Strings to List of Integers

#1
Easiest Way to Convert List of Hex Strings to List of Integers

<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;919072&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;legendonly&quot;:&quot;&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>
<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 <a href="https://blog.finxter.com/python-lists/" data-type="post" data-id="7332" target="_blank" rel="noreferrer noopener">Python list</a> of hexadecimal strings such as <code>['ff', 'ef', '0f', '0a', '93']</code>. How to convert it to a list of integers in Python such as <code>[255, 239, 15, 10, 147]</code>?</p>
<h2>Easiest Answer</h2>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="559" src="https://blog.finxter.com/wp-content/uploads/2022/11/image-266-1024x559.png" alt="" class="wp-image-919200" srcset="https://blog.finxter.com/wp-content/uploads/2022/11/image-266-1024x559.png 1024w, https://blog.finxter.com/wp-content/uplo...00x164.png 300w, https://blog.finxter.com/wp-content/uplo...68x419.png 768w, https://blog.finxter.com/wp-content/uplo...ge-266.png 1348w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<p class="has-base-background-color has-background">The easiest way to convert a list of hex strings to a list of integers in Python is the <a rel="noreferrer noopener" href="https://blog.finxter.com/list-comprehension/" data-type="post" data-id="1171" target="_blank">list comprehension</a> statement <code>[int(x, 16) for x in my_list]</code> that applies the built-in function <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-int-function/" data-type="post" data-id="22715" target="_blank">int()</a></code> to convert each hex string to an integer using the hexadecimal base <code>16</code>, and repeats this for each hex string <code>x</code> in the original list.</p>
<p>Here’s a minimal example:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">my_list = ['ff', 'ef', '0f', '0a', '93']
my_ints = [int(x, 16) for x in my_list] print(my_ints)
# [255, 239, 15, 10, 147]</pre>
<p>The <strong>list comprehension</strong> statement applies the expression <code>int(x, 16)</code> to each element <code>x</code> in the list <code>my_list</code> and puts the result of this expression in the <a href="https://blog.finxter.com/how-to-create-a-python-list/" data-type="post" data-id="10436" target="_blank" rel="noreferrer noopener">newly-created list</a>. </p>
<p>The <code><strong>int(x, 16)</strong></code> expression converts a <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">hex string to an integer</a> using the hexadecimal base argument <code>16</code>. A semantically identical way to write this would be <code>int(x, base=16)</code>. </p>
<p>In fact, there are many more ways to convert a hex string to an integer—each of them could be used in the <strong>expression part </strong>of the list comprehension statement. </p>
<p>However, I’ll show you one completely different approach to solving this problem without listing each and every combination of possible solutions. <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>For Loop with List Append</h2>
<p class="has-global-color-8-background-color has-background">You can <a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-create-an-empty-list-in-python/" data-type="post" data-id="453870" target="_blank">create an empty list</a> and add one hex integer at a time in the loop body after converting it from the hex string <code>x</code> using the <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-eval/" data-type="post" data-id="19204" target="_blank">eval('0x' + x)</a></code> function call. This first creates a hexadecimal string with <code>'0x'</code> prefix using <a rel="noreferrer noopener" href="https://blog.finxter.com/daily-python-puzzle-string-concatenation/" data-type="post" data-id="93" target="_blank">string concatenation</a> and then lets Python evaluate the string as if it was real code and not a string. </p>
<p>Here’s an example:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="3-5" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">my_list = ['ff', 'ef', '0f', '0a', '93'] my_ints = []
for x in my_list: my_ints.append(eval('0x' + x)) print(my_ints)
# [255, 239, 15, 10, 147]</pre>
<p>You use the fact that Python automatically converts a hex value of the form <code>0xff</code> to an integer <code>255</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="">>>> 0xff
255
>>> 0xfe
254
>>> 0x0f
15</pre>
<p>You may want to check out my in-depth guide on this important function for our solution:</p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f30d.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Recommended Tutorial</strong>: <a rel="noreferrer noopener" href="https://blog.finxter.com/python-eval/" data-type="post" data-id="19204" target="_blank">Python <code>eval()</code></a></p>
</div>


https://www.sickgaming.net/blog/2022/11/...-integers/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016