{"id":130003,"date":"2022-11-23T18:44:10","date_gmt":"2022-11-23T18:44:10","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=919072"},"modified":"2022-11-23T18:44:10","modified_gmt":"2022-11-23T18:44:10","slug":"easiest-way-to-convert-list-of-hex-strings-to-list-of-integers","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/11\/23\/easiest-way-to-convert-list-of-hex-strings-to-list-of-integers\/","title":{"rendered":"Easiest Way to Convert List of Hex Strings to List of Integers"},"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;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;}'>\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<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 <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>\n<h2>Easiest Answer<\/h2>\n<div class=\"wp-block-image\">\n<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\/uploads\/2022\/11\/image-266-300x164.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/11\/image-266-768x419.png 768w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/11\/image-266.png 1348w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n<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>\n<p>Here&#8217;s a minimal example:<\/p>\n<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']\nmy_ints = [int(x, 16) for x in my_list] print(my_ints)\n# [255, 239, 15, 10, 147]<\/pre>\n<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>\n<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>\n<p>In fact, there are many more ways to convert a hex string to an integer&#8212;each of them could be used in the <strong>expression part <\/strong>of the list comprehension statement. <\/p>\n<p>However, I&#8217;ll show you one completely different approach to solving this problem without listing each and every combination of possible solutions. <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>For Loop with List Append<\/h2>\n<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>\n<p>Here&#8217;s an example:<\/p>\n<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 = []\nfor x in my_list: my_ints.append(eval('0x' + x)) print(my_ints)\n# [255, 239, 15, 10, 147]<\/pre>\n<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>\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=\"\">>>> 0xff\n255\n>>> 0xfe\n254\n>>> 0x0f\n15<\/pre>\n<p>You may want to check out my in-depth guide on this important function for our solution:<\/p>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f30d.png\" alt=\"\ud83c\udf0d\" 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>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) Question: Given a Python list of hexadecimal strings such as [&#8216;ff&#8217;, &#8216;ef&#8217;, &#8216;0f&#8217;, &#8216;0a&#8217;, &#8217;93&#8217;]. How to convert it to a list of integers in Python such as [255, 239, 15, 10, 147]? Easiest Answer The easiest way to convert a list of hex strings to a list of integers in [&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-130003","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\/130003","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=130003"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/130003\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=130003"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=130003"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=130003"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}