Forums.ATC.no

Teknisk => Generelt teknisk => Emne startet av: ATC på 27. ſeptember 2008, 18:24 pm

Tittel: Perl - How to encode a string as hex (and decode it again)
Skrevet av: ATC27. ſeptember 2008, 18:24 pm
Use the pack() and unpack() functions. But you already knew that... the problem is that those functions aren't exactly self explanatory, are they?
Tittel: [Solved] Perl - How to encode a string as hex (and decode it again)
Skrevet av: ATC27. ſeptember 2008, 18:24 pm
The concept of "packing" vs. "unpacking" in Perl may seem a bit confusing because we consider clear text "unpacked" and hex "packed". When you think about it, this is ofcourse not really the case as the hex string will always be a hex string but the "clear text" can really be any binary data you can think of.

unpack("H*", "my string");  # = "6d7920737472696e67"
pack("H*", "6d7920737472696e67");  # = "my string"

By the way, don't think for a second that this can be useful for encrypting sensitive data. Hex is almost as easy to read as plaintext once you know the ASCII table.
Tittel: Sv: Perl - How to encode a string as hex (and decode it again)
Skrevet av: Floyd-ATC04. Mars 2014, 08:49 am
More pack/unpack fun

# Convert IP address in quad dotted decimal format to 32bit unsigned integer
Kode: [Velg]
perl -MIO::Socket -e 'print unpack("N", inet_aton("127.0.0.1")) . "\n";  # = 2130706433


# Convert 32bit unsigned integer to IP address in quad dotted decimal format
Kode: [Velg]
perl -MIO::Socket -e 'print inet_ntoa(pack("N", 2130706433)) . "\n";# = 127.0.0.1