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.
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.
1024
in decimal is not 0xFF
in hex. Instead, it is 0x400
.
You can use sprintf as:
my $hex = sprintf("0x%X", $d);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With