Forums.ATC.no
Teknisk => Generelt teknisk => Emne startet av: ATC på 27. ſ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?
-
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.
-
More pack/unpack fun
# Convert IP address in quad dotted decimal format to 32bit unsigned integer
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
perl -MIO::Socket -e 'print inet_ntoa(pack("N", 2130706433)) . "\n";
# = 127.0.0.1