converting a line of ascii to decimal

Discussion in 'Perl Misc' started by xtrimnx, Mar 9, 2005.

  1. xtrimnx

    xtrimnx Guest

    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;
    xtrimnx, Mar 9, 2005
    #1
    1. Advertising

  2. xtrimnx 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;


    while (<INPUT>) {
    print OUTPUT $_;
    s/(.)/unpack 'c', $1/eg;
    print OUTPUT $_;
    }

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Mar 9, 2005
    #2
    1. Advertising

  3. xtrimnx

    Ted Zlatanov Guest

    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
    Ted Zlatanov, Mar 9, 2005
    #3
  4. xtrimnx

    Jay Tilton Guest

    "xtrimnx" <> 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.

    printf '%*vd', ' ', $_;
    Jay Tilton, Mar 10, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ven
    Replies:
    3
    Views:
    1,296
  2. Gilbert Fine
    Replies:
    8
    Views:
    879
    Zentrader
    Aug 1, 2007
  3. Vitaliy
    Replies:
    1
    Views:
    455
    Peter Otten
    May 29, 2008
  4. valpa
    Replies:
    11
    Views:
    1,480
    Steven D'Aprano
    Mar 24, 2009
  5. Replies:
    0
    Views:
    266
Loading...

Share This Page