Python3 ASCII Hexadecimal to Binary String Conversion
<p>I'm using Python 3.2.3 on Windows, and am trying to convert binary data within a C-style ASCII file into its binary equivalent for later parsing using the struct module. For example, my input file contains "0x000A 0x000B 0x000C 0x000D", and I'd like to convert it into "\x00\x0a\x00\x0b\x00\x0c\x00\x0d".</p>

<p>The problem I'm running into is that the string datatypes have changed in Python 3, and the built-in functions to convert from hexadecimal to binary, such as <code>binascii.unhexlify()</code>, no longer accept regular unicode strings, but only byte strings. This process of converting from unicode strings to byte strings and back is confusing me, so I'm wondering if there's an easier way to achieve this. Below is what I have so far: </p>

<pre><code>with open(path, "r") as f:
    l = []
    data = f.read()
    values = data.split(" ")

    for v in values:
            if (v.startswith("0x")):
                    l.append(binascii.unhexlify(bytes(v[2:], "utf-8").decode("utf-8")

    string = ''.join(l)
</code></pre>
