Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use <code>int</code> to make this a lot easier. Function:</p> <pre><code>def hex_to_dex(strng_of_hex): return int(strng_of_hex, 16) </code></pre> <p>Example:</p> <pre><code>&gt;&gt;&gt; int("0xff", 16) 255 </code></pre> <p>It does not matter if you have a <code>0x</code> infront for int, it will ignore <code>0x</code> in the string. So, the following will also work:</p> <pre><code>&gt;&gt;&gt; int("a", 16) 10 </code></pre> <p>As for a raw hex code from scratch, try the following:</p> <pre><code>def hex(s): _hexer = "0123456789ABCDEF" return sum([_hexer.find(var) * 16 ** i for i, var in enumerate(reversed(s.upper()))]) </code></pre> <p>If you wanted to put some guards in:</p> <pre><code>def hex(s): _hexer = "0123456789ABCDEF" if not all([var in _hexer for var in s.upper()]): print "Invalid string" return None return sum([_hexer.find(var) * 16 ** i for i, var in enumerate(reversed(s.upper()))]) </code></pre> <p>I've used a lot of functions here, but for quick reference, here's an appendix:</p> <ol> <li><a href="http://docs.python.org/2/library/stdtypes.html#str.find" rel="nofollow">str.find</a></li> <li><a href="http://docs.python.org/2/whatsnew/2.3.html#pep-279-enumerate" rel="nofollow">enumerate</a></li> <li><a href="http://docs.python.org/2/library/functions.html#reversed" rel="nofollow">reversed</a> -> reverses the string</li> <li><a href="http://docs.python.org/2/library/functions.html#sum" rel="nofollow">sum</a></li> </ol> <p>As for the <code>[...]</code>, its a list comprehension, you can find loads of resources on that.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload