Search code examples
pythonio

Regular string of hex values to binary string


I am writing a file-data searching script and want to allow the user to search for a string of hex values (via argv). I had to switch from PHP to Python because the maximum value of integers are (too) limited on 32-bit PHP/machines. In PHP I could just do:

$SearchValue = pack('H*', $Input);

However, in Python there does not seem to be any equivalent for this format character. What are some alternatives for this conversion in Python, if any? The output needs to be a binary string (b'...').


Solution

  • 3>> import binascii
    3>> binascii.unhexlify('deadbeef')
    b'\xde\xad\xbe\xef'