Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Decimal to Hex Converter

by OverlordQ (Hermit)
on Aug 05, 2003 at 20:33 UTC ( #281153=CUFP: print w/ replies, xml ) Need Help??

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; }

Comment on Decimal to Hex Converter
Download Code
Re: Decimal to Hex Converter
by tedrek (Pilgrim) on Aug 06, 2003 at 04:27 UTC

    A few things I would like to point out. This doesn't actually convert a decimal number, it converts three decimal numbers each less than 255 into a hex sequence(decimal RGB -> Hex?). Also you don't use the modulus operator '%', which does a integer divide and returns the remainder, rather than having $f = $d / 16;$t = $f * 16; $s = $d - $t; you can just say $s = $d % 16;. And the final thing, rather than having a c-style for loop in convert (which you don't actually use) it would seem clearer to write

    foreach $num (@dtrip) {
        my $f = int($num / 16);
        ....
    

    But then again why not just handle any size number :) (with arbitrary base)

    BEGIN { my @map = (0..9, 'A'..'Z', 'a'..'z', qw/! @ $ % ^ & * ( ) _/); sub convert { my $num = shift; my $base = shift || 16; die "Base too big: Not enough characters." if $base > $#map; my $conv = ''; my $sign = $num > 0 ? '' : '-'; $num = abs $num; while ($num != 0) { $conv = $map[$num % $base] . $conv; $num = int($num / $base); } return $sign . $conv; } }
Re: Decimal to Hex Converter
by jmcnamara (Monsignor) on Aug 06, 2003 at 10:54 UTC

    You can use printf to do this with a lot less effort:
    perl -e 'printf "%02X%02X%02X\n", 44, 26, 127' Prints: 2C1A7F

    --
    John.

      Edit: Read my Edit2 :)
Re: Decimal to Hex Converter
by Juerd (Abbot) on Aug 06, 2003 at 14:45 UTC

    Even though Perl is a language where you can do a lot of different useful things without ever reading a manual, that doesn't mean you should. A good book would have taught you about printf, sprintf and pack.

    But you do use strict, so you must have read some introduction to Perl. What book or tutorial did you read?

    Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

      Actually I dont think I did read any of the perl books or tutorials when I wrote this, it was all picked up watching a friend code. I was just starting out with perl (Feb 2001) so that's why I use print, and simple math, back then I didn't know of printf, sprintf, pack, modulus, perl one-liners or anything of the sort.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://281153]
Approved by jeffa
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2014-12-28 00:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls