On 9 Mar 2005,
wrote:
> Okay, so I'm trying to read in the file and take each character one at
> at time and convert them to their numeric equivilants. So far this
> program just gets me the 1st charachter of each line and changes it.
> open OUTPUT,">fileout";
> open INPUT, "<filein"; while(<INPUT>) { $val = unpack('c', $_); print OUTPUT
> "$val\n"; $val2 = pack('U', $val); print OUTPUT "$val2\n"; }
> close INPUT;
> close OUTPUT;
The reason your way doesn't work is that the pack/unpack logic is
incorrect. Here's the right solution:
perl -n -e 'chomp; @out = unpack "c*", $_; print "@out\n"'
Basically, you don't need to repack the values (if you do, you'll just
get the original string), and you need an array instead of a scalar.
Here's an alternate solution:
perl -n -e 'chomp; @out = map { ord } split //, $_; print "@out\n"'
Ted