title: How to convert hexdump string of PNG data to binary in Python 3?
body: <p>I have a hexdump string of a small PNG file that I want to convert to binary for writing out a PNG file:</p>
  
  <pre><code>clip = "0x89 0x50 0x4e 0x47 0xd 0xa 0x1a 0xa 0x0 0x0 0x0 0xd 0x49 0x48 0x44 0x52 0x0 0x0 0x0 0x8 0x0 0x0 0x0 0x7 0x8 0x2 0x0 0x0 0x0 0xba 0x3b 0x9b 0x9 0x0 0x0 0x0 0x3 0x73 0x42 0x49 0x54 0x8 0x8 0x8 0xdb 0xe1 0x4f 0xe0 0x0 0x0 0x0 0x29 0x49 0x44 0x41 0x54 0x8 0x5b 0x63 0xfc 0xff 0xff 0x3f 0x3 0x36 0xc0 0x84 0x4d 0x10 0x24 0x46 0x2b 0x89 0xf7 0x1f 0xfe 0xfd 0xfc 0x9 0x75 0xb 0x8a 0x1d 0x39 0x95 0x9f 0xef 0x3e 0xfc 0xb 0x71 0xe 0x0 0xfc 0x18 0xd 0x6c 0xa9 0xf7 0x56 0x2d 0x0 0x0 0x0 0x0 0x49 0x45 0x4e 0x44 0xae 0x42 0x60 0x82 "
  </code></pre>
  
  <p>I have tried using <code>clip.encode("hex")</code> but get the error </p>
  
  <pre><code>'hex' is not a text encoding
  </code></pre>
  
  <p>Using <code>binascii.unhexlify(clip)</code>, I get </p>
  
  <pre><code>Odd-length string
  </code></pre>
  
  <p>And if I remove whitespace, the error is </p>
  
  <pre><code>Non-hexadecimal digit found
  </code></pre>
  
  <p>None of the other discussions on SO or the wider internet are helping.</p>