in reply to Reverse Hexadecimal Translation
# $dec - decimal number to convert to hex # $pad - (optional) how long the hex number must be (a minimum). If # it is not this long, it will be padded with zeros. # $x - (optional) pass 1 if you want the number to have a '0x' at the # beginning. sub toHex { my ($dec, $pad, $x) = @_; my $hex; my $str = "%"; $str = "0x" . $str if($x); $str .= "0" . $pad if($pad); $str .= "x"; $hex = sprintf($str, $dec); return $hex; } my $hex1 = toHex(15, 4, 1); my $hex2 = toHex(3045); my $hex3 = toHex(255, undef, 1); print "$hex1 $hex2 $hex3\n";Output:
0x000f be5 0xff
|
---|