Lucas Rey Lucas Rey - 1 year ago 125
Perl Question

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?

Answer Source
sprintf("%02X", $n)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download