dec2hex.pl to HTML.

index -|- end

Generated: Mon Aug 29 19:34:24 2016 from dec2hex.pl 2015/02/22 1.3 KB. text copy

#!/usr/bin/perl
use strict;
use warnings;
#print "Hello, World...\n";
my $in_decimal = 0;

sub dec2hex($) {
    my $decnum = shift;     # parameter passed to the subfunction
    my $hexnum = "";     # the final hex number
    my $tempval;
    while ($decnum != 0) {
      # get the remainder (modulus function)
      # by dividing by 16
      $tempval = $decnum % 16;
      # convert to the appropriate letter
      # if the value is greater than 9
      if ($tempval > 9) {
         $tempval = chr($tempval + 55);
      }
      # 'concatenate' the number to 
      # what we have so far in what will
      # be the final variable
      $hexnum = $tempval . $hexnum ;
      # new actually divide by 16, and 
      # keep the integer value of the 
      # answer
      $decnum = int($decnum / 16); 
      # if we cant divide by 16, this is the
      # last step
      if ($decnum < 16) {
         # convert to letters again..
         if ($decnum > 9) {
            $decnum = chr($decnum + 55);
         }
         # add this onto the final answer.. 
         # reset decnum variable to zero so loop
         # will exit
         $hexnum = $decnum . $hexnum; 
         $decnum = 0 
      }
    }
    return $hexnum;
} # end sub

if (@ARGV) {
   $in_decimal = pop @ARGV;
   print "Decimal [$in_decimal] = hex [" . dec2hex($in_decimal) . "] ...\n";
} else {
   print "Usage: $0 decimal\n";
}
exit(0);

index -|- top

checked by tidy  Valid HTML 4.01 Transitional