views:

62

answers:

2

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

+10  A: 

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
cjm
+2  A: 
$ perldoc -f hex
…
To present something as hex,
look into "printf", "sprintf", or "unpack".

==>

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