foreach my $chars (split(//,$input_string)) { $hexchars .= sprintf "%x", ord($chars); } #### $ perl -le ' $str = qq{test\r\n}; $wrong = join q{}, map { sprintf q{%x}, ord } split m{}, $str; print $wrong; $right = join q{}, map { sprintf q{%02x}, ord } split m{}, $str; print $right;' 74657374da 746573740d0a