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

Is their a way to print a hex number without it being automaticly converted into a decimal? Something like:

$hex_num = 0xf4a; print $hex_num; # but no conversion

Replies are listed 'Best First'.
(zdog) Re: Output numbers in hex
by zdog (Priest) on Jul 08, 2002 at 07:09 UTC
    This may be what you're looking for:

    printf ("%x", 0xf4a);

    See sprintf and printf for more information.

    Zenon Zabinski | zdog | zdog@perlmonk.org

Re: Output numbers in hex
by Abigail-II (Bishop) on Jul 08, 2002 at 10:17 UTC
    Actually, all you want is to keep your hex constant as a string.
    $hex_num = "0xf4a"; print $hex_num;
    When you are ready to use the hex number in any numerical calculations, don't forget to use the hex function.

    Abigail

Re: Output numbers in hex
by seattlejohn (Deacon) on Jul 08, 2002 at 07:11 UTC
    The variable $hex_num doesn't "know" that the number is in hex, just that it's an integer. Your code as written is actually equivalent to this:
    $hex_num = 3914; print $hex_num;

    What you might want is this:

    $hex_num = 0xf4a; printf "0x%x", $hex_num;