#!/usr/bin/perl -w use CGI; use CGI::Carp "fatalsToBrowser"; use strict; my %map = (0 => '0', 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7, 8 => 8, 9 => 9, 10 => 'A', 11 => 'B', 12 => 'C', 13 => 'D', 14 => 'E', 15 => 'F'); my (@dtrip, @conv, @conv2, @conv3, @conv4); my $cgi = new CGI; print $cgi->header(); unless ($cgi->param('doit')) { print< Decimal to Hex Converter

Decimal to Hex Converter

Please enter the decimal triplet, eg: 44,26,127


EOT } else { if($cgi->param('doit')) { my $triplet = $cgi->param('doit'); my $converted = &convert($triplet); print< Decimal to Hex Converter

Decimal to Hex Converter

Converting @dtrip

EOT $conv[0] = int($dtrip[0]/16); $conv[1] = int($dtrip[1]/16); $conv[2] = int($dtrip[2]/16); print "\t\t" . '' . "\n"; print "\t\t" . '' . "\n"; print "\t\t" . '' . "\n"; print< EOT $conv2[0] = $conv[0] * 16; $conv2[1] = $conv[1] * 16; $conv2[2] = $conv[2] * 16; print "\t\t" . '' . "\n"; print "\t\t" . '' . "\n"; print "\t\t" . '' . "\n"; print< EOT $conv3[0] = $dtrip[0] - $conv2[0]; $conv3[1] = $dtrip[1] - $conv2[1]; $conv3[2] = $dtrip[2] - $conv2[2]; print "\t\t" . '' . "\n"; print "\t\t" . '' . "\n"; print "\t\t" . '' . "\n"; print< EOT $conv4[0] = $map{$conv[0]}; $conv4[1] = $map{$conv3[0]}; $conv4[2] = $map{$conv[1]}; $conv4[3] = $map{$conv3[1]}; $conv4[4] = $map{$conv[2]}; $conv4[5] = $map{$conv3[2]}; print "\t\t" . '' . "\n"; print "\t\t" . '' . "\n"; print "\t\t" . '' . "\n"; print<
Converting $dtrip[0] Converting $dtrip[1] Converting $dtrip[2]
' . "$dtrip[0] / 16 = $conv[0]" . '' . "$dtrip[1] / 16 = $conv[1]" . '' . "$dtrip[2] / 16 = $conv[2]" . '
' . "$conv[0] * 16 = $conv2[0]" . '' . "$conv[1] * 16 = $conv2[1]" . '' . "$conv[2] * 16 = $conv2[2]" . '
' . "$dtrip[0] - $conv2[0] = $conv3[0]" . '' . "$dtrip[1] - $conv2[1] = $conv3[1]" . '' . "$dtrip[2] - $conv2[2] = $conv3[2]" . '
' . "$conv[0] and $conv3[0] = \n $conv4[0]$conv4[1]" . '' . "$conv[1] and $conv3[1] = \n $conv4[2]$conv4[3]" . '' . "$conv[2] and $conv3[2] = \n $conv4[4]$conv4[5]" . '

Final Hex is: EOT for(my $i=0;$i<=6; $i++) { print "$conv4[$i]"; } print<

EOT } } sub convert { my $triplet = $_[0]; my $converted; @dtrip = split(/\,/, $triplet); for(my $i=1; $i<4; $i++) { my $j = $i - 1; my $first = int($dtrip[$j] / 16); my $temp = $first * 16; my $second = $dtrip[$j]-$temp; my $hex1 = $map{$first}; my $hex2 = $map{$second}; $converted .= "$hex1$hex2"; } return $converted; }