foreach my $chars (split(//,$input_string)) { $hexchars .= sprintf "%x", ord($chars); }
Be aware that the format %x will cause you problems if your string contains characters with ordinals in the range 0x00 through 0x0f. Consider the following code where the string is terminated with CRLF.
$ 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
You can see that %x will use a variable width depending on the value to be formatted and so, for single digit values, doesn't show a leading zero. Conversely, %02x does because it is specifying a width of 2 characters, padding with leading zeros as necessary.
I hope this is of interest.
Cheers,
JohnGG
In reply to Re: Process a set num of characters at a time
by johngg
in thread Process a set num of characters at a time
by learningperl01
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |