Perl Hexadecimal Conversion

Perl Hexadecimal Conversion

Perl Hexadecimal Conversion

Converting RGB to hex just involves understanding the decimal representation of RGB. See below for a small perl procedure.

sub RGB2Hex
{
my $r = shift;
my $g = shift;
my $b = shift;
my $dec = 0;

$dec += 256*256*$r;
$dec += 256*$g;
$dec += $b;

return sprintf(“%.6x”, $dec);
}

Well here's a dead simple way of converting betwen hex and dec in perl..
#!/usr/bin/perl $foo = 10; $hexval = sprintf("%x", $foo); $decval = hex($hexval); print "n$foo in hex is $hexval and in dec is $decvaln"; # end
Cut and paste the above code into a new file, save it as hexdec.pl or dechex.pl or whatever and ensure that you have execute permissions. The script creates a variable, $foo with a value of your choice, (in this case 10). It will then print out it's value in hexadecimal and in decimal.

Christo

follow me on twitter to catch all my tech updates: http://www.twitter.com/planet_guru

Relevant pages: