5

Given the variable $foo containing binary data, how do I get the hexadecimal representation of $foo in Perl?

2 Answers 2

12

If $foo is a string containing arbitrary data, use unpack:

my $hex = unpack('H*', $foo);

If it's just an integer, use sprintf:

my $hex = sprintf('%x', $foo); # Or %X if you want upper-case letters
2
$ perldoc -f hex
…
To present something as hex,
look into "printf", "sprintf", or "unpack".

==>

$hex = sprintf("%x", $foo);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.