Lucas Rey
Lucas Rey

Reputation: 467

Convert number to HEX in perl

I need to convert a number to hex in perl. I'm using the following simple code:

my $n = 10;
print sprintf("%X", $n);

That will output: A That's correct, but I need always 2 bytes like: 0A

So for example: 90 will show 5A - 6 will show 06 - 10 will show 0A

How can I do that?

Upvotes: 4

Views: 4270

Answers (1)

ikegami
ikegami

Reputation: 385546

sprintf("%02X", $n)

Upvotes: 4

Related Questions