Use the X, x and # format codes in printf and sprintf:
$num = 15;
printf("%X\n", $num); # F
printf("%x\n", $num); # f
printf("%02x\n", $num); # 0f
printf("%#x\n", $num); # 0xf
printf("0x%02x\n", $num); # 0x0f
The last example is excessive but it forces formatting for zero:
printf("%#x\n", 0); # 0
printf("0x%02x\n", 0); # 0x00
| [reply] |
sprintf "%x", $num;
| [reply] [d/l] |
Alternatively, to get the hex in a variable:
$octet = 192;
$out = pack "c", $octet;
$hex = unpack "H2", $out;
Edited by davido: Added code tags. | [reply] [d/l] |