in reply to Reverse Hexadecimal Translation

The sprintf() method: my $hex_number = sprintf "%lx", $number; That's the best way I know. Another option would be:
my %table = ( 'a' => '10', 'b' => '11', 'c' => '12', 'd' => '13', 'e' => '14', 'f' => '15', ); my @numbers = ('a0', '11', 'ff', '9084fe', '10'); foreach my $trial (@numbers) { my @digits = split //, $trial; my $result = 0; foreach my $digit (@digits) { $result *= 16; $result += $digit =~ /\d/ ? $digit : $table{lc($digit)}; } print "$trial is actually $result\n"; }
I wouldn't do it that way very often, though. The internal conversion routine is much faster.

modified on 9 April 'cuz btrott is right in his comment, that sneaky japh.

Replies are listed 'Best First'.
RE: Re: Reverse Hexadecimal Translation
by btrott (Parson) on Apr 10, 2000 at 05:32 UTC
    chromatic wrote:
    > my $dec_number = sprintf "%lx", $number;
    Aren't your variables named backwards here? This sprintf format does decimal->hex translation; your variables make it look like hex->decimal translation.