Massyn has asked for the wisdom of the Perl Monks concerning the following question:

Greeting fellow monks, The hex() function returns the decimal value of a hexadecimal value. Now how do I get the reverse of that?
$decimal = 1234; $hex = 0x$decimal; print "Hex of $decimal = $hex\n";
Unfortunatly, this doesn't work. Any ideas? Thanks!

Replies are listed 'Best First'.
(jeffa) Re: Reverse of hex?
by jeffa (Bishop) on Aug 02, 2002 at 15:46 UTC
    sprintf with %x is one way:
    my $hex = sprintf("%x",$decimal);
    You can use pack/unpack as well:
    my $hex = unpack 'H*',pack('N',$decimal);
    but you will probably want to trim the leading zeroes.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)