Frank Krueger
Frank Krueger

Reputation: 70983

How do you print hexadecimal digits to a Perl string?

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?

Upvotes: 2

Views: 995

Answers (2)

Brad Gilbert
Brad Gilbert

Reputation: 34120

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

my $rgb = '#' . uc unpack 'H6', pack 'C3', $r, $g, $b;

Upvotes: 3

Sinan Ünür
Sinan Ünür

Reputation: 118128

my $rgb = sprintf '#%02X%02X%02X', $r, $g, $b;

See sprintf and printf.

Upvotes: 11

Related Questions