[Tut] Python Convert Hex to Base64 - 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] Python Convert Hex to Base64 (/thread-100318.html) |
[Tut] Python Convert Hex to Base64 - xSicKxBot - 12-01-2022 Python Convert Hex to Base64 <div> <div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload='{"align":"left","id":"936986","slug":"default","valign":"top","ignore":"","reference":"auto","class":"","count":"1","legendonly":"","readonly":"","score":"5","best":"5","gap":"5","greet":"Rate this post","legend":"5\/5 - (1 vote)","size":"24","width":"142.5","_legend":"{score}\/{best} - ({count} {votes})","font_factor":"1.25"}'> <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>: How to convert a hexadecimal string such as <code>02af01ff00</code> to a normal string using the Base64 format in Python?</p> <div class="wp-block-image"> <figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="682" src="https://blog.finxter.com/wp-content/uploads/2022/11/image-309-1024x682.png" alt="" class="wp-image-937206" srcset="https://blog.finxter.com/wp-content/uploads/2022/11/image-309-1024x682.png 1024w, https://blog.finxter.com/wp-content/uploads/2022/11/image-309-300x200.png 300w, https://blog.finxter.com/wp-content/uploads/2022/11/image-309-768x512.png 768w, https://blog.finxter.com/wp-content/uploads/2022/11/image-309.png 1354w" 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/1f449.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Short answer</strong>: Use the following fancy one-liner expression to convert the hex string <code>s</code> to a Base64-encoded Python string: <code>base64.b64encode(bytes.fromhex(s)).decode()</code>.</p> <p>For the long answer, keep reading! <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f978.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p> <p>If you’re like me, you may need a quick refresher on <strong>how Base64 encoding works</strong> and what it is exactly. Although I studied computer science a couple of years ago, I don’t have all those super basic “bits” of knowledge at the top of my head all the time.</p> <p>You may already know about Base64 — in that case, I’d recommend you skip the next section and jump ahead right away. <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>What Is Base64 Encoding?</h2> <p class="has-global-color-8-background-color has-background">Base64 is a very minimal encoding where a minimal set of characters — <code>A-Z</code>, <code>a-z</code>, and <code>0-9</code>, essentially — are encoded using only six bits. Each bit position doubles the number of different encodings, so the Base64 encoding can encode <strong>2*2*2*2*2*2 = 2^6 = 64</strong> different characters. </p> <p>Here’s the whole table — fortunately, the encoding is small and efficient enough that I can show you the whole thing! <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f92f.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> </p> <p><em>(And, no, Emojis don’t have any place in Base64, it’s VERY old school!)</em></p> <figure class="wp-block-table is-style-stripes"> <table> <thead> <tr> <th>Index</th> <th>Binary</th> <th>Char</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>000000</td> <td>A</td> </tr> <tr> <td>1</td> <td>000001</td> <td>B</td> </tr> <tr> <td>2</td> <td>000010</td> <td>C</td> </tr> <tr> <td>3</td> <td>000011</td> <td>D</td> </tr> <tr> <td>4</td> <td>000100</td> <td>E</td> </tr> <tr> <td>5</td> <td>000101</td> <td>F</td> </tr> <tr> <td>6</td> <td>000110</td> <td>G</td> </tr> <tr> <td>7</td> <td>000111</td> <td>H</td> </tr> <tr> <td>8</td> <td>001000</td> <td>I</td> </tr> <tr> <td>9</td> <td>001001</td> <td>J</td> </tr> <tr> <td>10</td> <td>001010</td> <td>K</td> </tr> <tr> <td>11</td> <td>001011</td> <td>L</td> </tr> <tr> <td>12</td> <td>001100</td> <td>M</td> </tr> <tr> <td>13</td> <td>001101</td> <td>N</td> </tr> <tr> <td>14</td> <td>001110</td> <td>O</td> </tr> <tr> <td>15</td> <td>001111</td> <td>P</td> </tr> <tr> <td>16</td> <td>010000</td> <td>Q</td> </tr> <tr> <td>17</td> <td>010001</td> <td>R</td> </tr> <tr> <td>18</td> <td>010010</td> <td>S</td> </tr> <tr> <td>19</td> <td>010011</td> <td>T</td> </tr> <tr> <td>20</td> <td>010100</td> <td>U</td> </tr> <tr> <td>21</td> <td>010101</td> <td>V</td> </tr> <tr> <td>22</td> <td>010110</td> <td>W</td> </tr> <tr> <td>23</td> <td>010111</td> <td>X</td> </tr> <tr> <td>24</td> <td>011000</td> <td>Y</td> </tr> <tr> <td>25</td> <td>011001</td> <td>Z</td> </tr> <tr> <td>26</td> <td>011010</td> <td>a</td> </tr> <tr> <td>27</td> <td>011011</td> <td>b</td> </tr> <tr> <td>28</td> <td>011100</td> <td>c</td> </tr> <tr> <td>29</td> <td>011101</td> <td>d</td> </tr> <tr> <td>30</td> <td>011110</td> <td>e</td> </tr> <tr> <td>31</td> <td>011111</td> <td>f</td> </tr> <tr> <td>32</td> <td>100000</td> <td>g</td> </tr> <tr> <td>33</td> <td>100001</td> <td>h</td> </tr> <tr> <td>34</td> <td>100010</td> <td>i</td> </tr> <tr> <td>35</td> <td>100011</td> <td>j</td> </tr> <tr> <td>36</td> <td>100100</td> <td>k</td> </tr> <tr> <td>37</td> <td>100101</td> <td>l</td> </tr> <tr> <td>38</td> <td>100110</td> <td>m</td> </tr> <tr> <td>39</td> <td>100111</td> <td>n</td> </tr> <tr> <td>40</td> <td>101000</td> <td>o</td> </tr> <tr> <td>41</td> <td>101001</td> <td>p</td> </tr> <tr> <td>42</td> <td>101010</td> <td>q</td> </tr> <tr> <td>43</td> <td>101011</td> <td>r</td> </tr> <tr> <td>44</td> <td>101100</td> <td>s</td> </tr> <tr> <td>45</td> <td>101101</td> <td>t</td> </tr> <tr> <td>46</td> <td>101110</td> <td>u</td> </tr> <tr> <td>47</td> <td>101111</td> <td>v</td> </tr> <tr> <td>48</td> <td>110000</td> <td>w</td> </tr> <tr> <td>49</td> <td>110001</td> <td>x</td> </tr> <tr> <td>50</td> <td>110010</td> <td>y</td> </tr> <tr> <td>51</td> <td>110011</td> <td>z</td> </tr> <tr> <td>52</td> <td>110100</td> <td>0</td> </tr> <tr> <td>53</td> <td>110101</td> <td>1</td> </tr> <tr> <td>54</td> <td>110110</td> <td>2</td> </tr> <tr> <td>55</td> <td>110111</td> <td>3</td> </tr> <tr> <td>56</td> <td>111000</td> <td>4</td> </tr> <tr> <td>57</td> <td>111001</td> <td>5</td> </tr> <tr> <td>58</td> <td>111010</td> <td>6</td> </tr> <tr> <td>59</td> <td>111011</td> <td>7</td> </tr> <tr> <td>60</td> <td>111100</td> <td>8</td> </tr> <tr> <td>61</td> <td>111101</td> <td>9</td> </tr> <tr> <td>62</td> <td>111110</td> <td>+</td> </tr> <tr> <td>63</td> <td>111111</td> <td>/</td> </tr> </tbody> <tfoot> <tr> <td><strong>Index</strong></td> <td><strong>Binary</strong></td> <td><strong>Character</strong></td> </tr> </tfoot> </table> </figure> <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-base64/" data-type="post" data-id="327003" target="_blank">Python Base64 – String Encoding and Decoding [+Video]</a></p> <h2>How to Convert Base64 Encoding (Hex String) to Human-Readable String in Python?</h2> <p>You can convert a hex string of the format <code>'02af01ff00'</code> to a Base64 encoded normal Python string by using the expression: </p> <p><code>base64.b64encode(bytes.fromhex(s)).decode()</code></p> <p>You can convert the resulting Base64 string back to a normal string by using the one-liner expression:</p> <p><code>base64.b64decode(b64.encode()).hex()</code></p> <p>Here’s a code example—I’ll break it down for you right after the code:</p> <pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="6" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import base64 s = '02af01ff00' # hex string -> base64 string b64 = base64.b64encode(bytes.fromhex(s)).decode() # base64 string -> hex string s2 = base64.b64decode(b64.encode()).hex() print(s) print(b64) print(s2) </pre> <p>The output shows that you successfully converte from the hex string to the Base64 string and back to the hex string:</p> <pre class="wp-block-preformatted"><code>02af01ff00 Aq8B/wA= 02af01ff00</code></pre> <p>You can see that the start and end values of the conversion remain the same.</p> <p>Let’s break down the code step by step!</p> <p><strong>Step 1</strong>: The initial hex string is still in a non-standardized format <code>'02af01ff00'</code>. We require it to be a <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-bytes-vs-bytearray/" data-type="post" data-id="870390" target="_blank">bytes</a></code> object because this is the required input format of the <code>base64</code> functions shown in a moment. You use the <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-convert-hex-string-to-binary/" data-type="post" data-id="847708" target="_blank">bytes.fromhex()</a></code> function.</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="">>>> s = '02af01ff00' >>> bytes.fromhex(s) b'\x02\xaf\x01\xff\x00'</pre> <p><strong>Step 2</strong>: You use the <code>base64.b64encode()</code> function to take the hex string (as <code>bytes</code> object) and convert it to a <code>bytes</code> object in Base64 encoding. This is almost what you want — but it’s not yet a normal Python string!</p> <pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="1" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> base64.b64encode(bytes.fromhex(s)) b'Aq8B/wA='</pre> <p><strong>Step 3</strong>: To convert the <code>bytes</code> object in Base64 encoding to a normal Python string, we use the <code>bytes.decode()</code> method.</p> <pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="1" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> base64.b64encode(bytes.fromhex(s)).decode() 'Aq8B/wA='</pre> <p>Voilà, exactly what you wanted! But how to convert it back?</p> <p><strong>Step 4 and 5</strong>: You can convert the normal Base64-encoded Python string back to the hex string from the beginning by using the <code>string.encode()</code> method to obtain a <code>bytes</code> object, passing it into the <code>base64.b64decode()</code> function to obtain a Base64 <code>bytes</code> representation, and converting it to a hexadecimal string by using the <code><a href="https://blog.finxter.com/how-to-convert-hex-string-to-bytes-in-python/" data-type="post" data-id="843354" target="_blank" rel="noreferrer noopener">bytes.hex()</a></code> method.</p> <pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="3" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> base64.b64decode('Aq8B/wA='.encode()) b'\x02\xaf\x01\xff\x00' >>> base64.b64decode('Aq8B/wA='.encode()).hex() '02af01ff00'</pre> <h2>Thanks <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;" /></h2> <p>Thanks for reading through the whole tutorial, I hope you managed to solve your issue! If not, you can check out <a rel="noreferrer noopener" href="https://stackoverflow.com/a/63595341/16045637" data-type="URL" data-id="https://stackoverflow.com/a/63595341/16045637" target="_blank">this</a> highly interesting SO answer.</p> <p>Also, make sure to check out our free Python cheat sheets for maximal learning efficiency and fun! </p> </div> https://www.sickgaming.net/blog/2022/11/29/python-convert-hex-to-base64/ |