tags:

views:

65

answers:

2

I have three integers:

($r, $g, $b) = (255, 128, 0);

I would like to print a string like:

"#FF8000"

using those variables.

How do I do this?

+11  A: 
my $rgb = sprintf '#%02X%02X%02X', $r, $g, $b;

See sprintf and printf.

Sinan Ünür
Excellent, thank you.
Frank Krueger
+2  A: 

You could use pack and unpack, to get the hexadecimal string.

my $rgb = '#' . uc unpack 'H6', pack 'C3', $r, $g, $b;
Brad Gilbert
Interesting. I'm more comfortable with the C-like syntax, but thanks for the other perspective.
Frank Krueger