Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert decimal to hexadecimal in Perl?

Tags:

perl

How can I convert a number, $d = 1024, in decimal to 0xFF in hex in Perl?

The d variable needs to be assigned to a different variable and be printed, so for readability I required it to be in hexadecimal format.

like image 322
Alisha Avatar asked May 07 '12 11:05

Alisha


People also ask

How do you convert a decimal number to hexadecimal?

Take decimal number as dividend. Divide this number by 16 (16 is base of hexadecimal so divisor here). Store the remainder in an array (it will be: 0 to 15 because of divisor 16, replace 10, 11, 12, 13, 14, 15 by A, B, C, D, E, F respectively). Repeat the above two steps until the number is greater than zero.


1 Answers

1024 in decimal is not 0xFF in hex. Instead, it is 0x400.

You can use sprintf as:

my $hex = sprintf("0x%X", $d); 
like image 198
codaddict Avatar answered Sep 27 '22 18:09

codaddict