$hex = sprintf "%02x", ord($char); # but why not just do it all in one line printf "%s %02x\n", $char, ord($char);