Perl: Decimal to 32bits hex convert

1

I want to convert decimal number 64 into hex representation: 0x00000040. I am using

printf("0x%X", 64);

but it gives output: 0x40. Can anyone please help me how to represent the decimal number in 0x00000000 format?

perl
hex
decimal
asked on Stack Overflow May 3, 2018 by Niraj • edited May 3, 2018 by simbabque

1 Answer

4

You can specify the length of the field between the % and the X (e.g. %8X). By default, the number will be padded with spaces, but using a leading zero for the length (e.g. %08X) will cause printf to pad with zeroes instead. Therefore, the following can be used:

printf("0x%08X", 64);
answered on Stack Overflow May 3, 2018 by Niraj • edited May 3, 2018 by ikegami

User contributions licensed under CC BY-SA 3.0