Nowayz Nowayz - 5 months ago 42
Perl Question

How to convert a hex-encoded string to a byte string in Perl?

My original code is in Python, but I need to convert it to Perl for some libraries that I don't have at my disposal in Python.

In Python I would do this:

packet=binascii.unhexlify('F0000000F6905C452001A8C0000000000160994E810FB54E0100DB0000000000000')


AND

This would create a string containing the binary representation of:

0xF0 0x00 0x00 0x00 0xF6 0x90 0x5C 0x45 etc...


Now that my string is a byte array I can send it as the payload for my packet. How do I do it Perl?

Mat Mat
Answer

You can use the pack function for this.

Example:

$ perl -e 'print pack("H*", "303132616263"), "\n";'
012abc

Check out the pack tutorial.