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

10,125

You can use the pack function for this.

Example:

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

Check out the pack tutorial.

Share:
10,125
Nowayz
Author by

Nowayz

Updated on June 17, 2022

Comments

  • Nowayz
    Nowayz almost 2 years

    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?