http://www.perlmonks.org?node_id=281153

This was one of my own hand-crafted projects, it's pretty bad (doesn't do any error checking for erroneous input), but it works like it should :) and it even shows you the handy math involved :

Edit: I forgot to point it out the first time this is a Decimal to Hex RGB Converter, the only people I showed it to before, were told it was a RGB Converter, sorry.

Edit 2: Again I apologise for not using printf, sprintf, or pack, but when I coded this (Feb 2001) I had only even touched perl for a month or two at most.

#!/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<<EOT; <html> <head> <title>Decimal to Hex Converter</title> </head> <body bgcolor="black" text="white"> <center> <h1>Decimal to Hex Converter</h1> </center> <p>Please enter the decimal triplet, eg: 44,26,127<BR> <form action="hex.cgi" method="post"> <p><input type="text" name="doit" ><br> <input type="submit" value="Convert"> </form> </body> </html> EOT } else { if($cgi->param('doit')) { my $triplet = $cgi->param('doit'); my $converted = &convert($triplet); print<<EOT; <html> <head> <title>Decimal to Hex Converter</title> <body bgcolor="black" text="white"> <center> <h1>Decimal to Hex Converter</h1> </center> <center> <h3>Converting @dtrip</h3> </center> <center> <table cellpadding=10> <tr> <td>Converting $dtrip[0]</td> <td>Converting $dtrip[1]</td> <td>Converting $dtrip[2]</td> </tr> <tr> EOT $conv[0] = int($dtrip[0]/16); $conv[1] = int($dtrip[1]/16); $conv[2] = int($dtrip[2]/16); print "\t\t" . '<td>' . "$dtrip[0] / 16 = $conv[0]" . '</td>' . "\n"; print "\t\t" . '<td>' . "$dtrip[1] / 16 = $conv[1]" . '</td>' . "\n"; print "\t\t" . '<td>' . "$dtrip[2] / 16 = $conv[2]" . '</td>' . "\n"; print<<EOT; </tr> <tr> EOT $conv2[0] = $conv[0] * 16; $conv2[1] = $conv[1] * 16; $conv2[2] = $conv[2] * 16; print "\t\t" . '<td>' . "$conv[0] * 16 = $conv2[0]" . '</td>' . "\n"; print "\t\t" . '<td>' . "$conv[1] * 16 = $conv2[1]" . '</td>' . "\n"; print "\t\t" . '<td>' . "$conv[2] * 16 = $conv2[2]" . '</td>' . "\n"; print<<EOT; </tr> <tr> EOT $conv3[0] = $dtrip[0] - $conv2[0]; $conv3[1] = $dtrip[1] - $conv2[1]; $conv3[2] = $dtrip[2] - $conv2[2]; print "\t\t" . '<td>' . "$dtrip[0] - $conv2[0] = $conv3[0]" . '</td>' +. "\n"; print "\t\t" . '<td>' . "$dtrip[1] - $conv2[1] = $conv3[1]" . '</td>' +. "\n"; print "\t\t" . '<td>' . "$dtrip[2] - $conv2[2] = $conv3[2]" . '</td>' +. "\n"; print<<EOT; </tr> <tr> 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" . '<td>' . "$conv[0] and $conv3[0] = \n $conv4[0]$conv4[1 +]" . '</td>' . "\n"; print "\t\t" . '<td>' . "$conv[1] and $conv3[1] = \n $conv4[2]$conv4[3 +]" . '</td>' . "\n"; print "\t\t" . '<td>' . "$conv[2] and $conv3[2] = \n $conv4[4]$conv4[5 +]" . '</td>' . "\n"; print<<EOT; </tr></table> <center><h3>Final Hex is: EOT for(my $i=0;$i<=6; $i++) { print "$conv4[$i]"; } print<<EOT; </h3></center> </body> </html> 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; }