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

I know there is a function that converts a hex number into an integer, but for the life of me I can't find anything that does the reverse. All I have is a integer between 0 and 255 and want to print the equivalent hex value.

I read the perldoc, and searched here at perlmonks but I couldn't find what I needed. I really hope I didn't miss something obvious. Any help I can get would be great.

Fenonn

Replies are listed 'Best First'.
(bbfu) Re: Integer to Hex?
by bbfu (Curate) on Jun 09, 2001 at 01:25 UTC

    sprintf, specifically:

    $hex = sprintf "%x", $integer.

    bbfu
    Seasons don't fear The Reaper.
    Nor do the wind, the sun, and the rain.
    We can be like they are.

Re: Integer to Hex?
by Big Willy (Scribe) on Jun 11, 2001 at 17:50 UTC
    Assuming $integer stores your integer, this would probably produce a string scalar with the hex value in it:
    $hex = unpack("H*", $integer);
    Of course you could omit the assignment to $hex and simply print the return of unpack too.
Re: Integer to Hex?
by Daddio (Chaplain) on Jun 09, 2001 at 01:35 UTC

    Update: Oops, oops, oops...I guess I didn't read the question well enough. That's what I get for trying a reply after this kind of week. Reply withdrawn.

    D a d d i o

      No, it doesn't. hex interprets its argument as being a hex number. But what was asked was to get a hex representation of a number.

      BTW, your $dec_num is misleading. hex just returns a number. It doesn't make much sense calling that a decimal number. "decimal" is relevant when we talk about representing numbers - for instance when reading in a number, or printing it. But all we do here is storing it. Most likely in a binary format - but which format it is, is irrelevant on the level of the language.

      -- Abigail