title: decoding base64 guid in python
body: <p>I am trying to convert a base64 string back to a GUID style hex number in python and having issues.</p>
  
  <p>Base64 encoded string is: <code>bNVDIrkNbEySjZ90ypCLew==</code></p>
  
  <p>And I need to get it back to: <code>2243d56c-0db9-4c6c-928d-9f74ca908b7b</code></p>
  
  <p>I can do it with the following PHP code but can't work out how to to it in Python</p>
  
  <pre><code>function Base64ToGUID($guid_b64) {
      $guid_bin = base64_decode($guid_b64);
  
      return join('-', array(
        bin2hex(strrev(substr($guid_bin, 0, 4))),
        bin2hex(strrev(substr($guid_bin, 4, 2))),
        bin2hex(strrev(substr($guid_bin, 6, 2))),
        bin2hex(substr($guid_bin, 8, 2)),
        bin2hex(substr($guid_bin, 10, 6))
      ));
    }
  </code></pre>
  
  <p>Here is the GUIDtoBase64 version:</p>
  
  <pre><code>function GUIDToBase64($guid) {
      $guid_b64 = '';
      $guid_parts = explode('-', $guid);
  
      foreach ($guid_parts as $k =&gt; $part) {
        if ($k &lt; 3)
          $part = join('', array_reverse(str_split($part, 2)));
  
        $guid_b64 .= pack('H*', $part);
      }
  
      return base64_encode($guid_b64);
    }
  </code></pre>
  
  <p>Here are some of the results using some of the obvious and not so obvious options:</p>
  
  <pre><code>import base64
  import binascii
  &gt;&gt;&gt; base64.b64decode("bNVDIrkNbEySjZ90ypCLew==")
  'l\xd5C"\xb9\rlL\x92\x8d\x9ft\xca\x90\x8b{'
  &gt;&gt;&gt; binascii.hexlify(base64.b64decode("bNVDIrkNbEySjZ90ypCLew=="))
  '6cd54322b90d6c4c928d9f74ca908b7b'
  </code></pre>