Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > converting a line of ascii to decimal

Reply
Thread Tools

converting a line of ascii to decimal

 
 
xtrimnx
Guest
Posts: n/a
 
      03-09-2005
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;

 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      03-09-2005
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
 
Reply With Quote
 
 
 
 
Ted Zlatanov
Guest
Posts: n/a
 
      03-09-2005
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
 
Reply With Quote
 
Jay Tilton
Guest
Posts: n/a
 
      03-10-2005
"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', ' ', $_;

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
decimal.Decimal formatting python@lists.fastmail.net Python 0 07-19-2010 12:32 PM
how to convert from Decimal('1.23456789') to Decimal('1.234') valpa Python 11 03-24-2009 07:11 AM
Error: Cannot convert Decimal("0.0000") to Decimal Vitaliy Python 1 05-29-2008 10:36 AM
TypeError: unsupported operand type(s) for -: 'Decimal' and 'Decimal'. Why? Gilbert Fine Python 8 08-01-2007 01:58 AM
Decimal to Packed Decimal Conversion in C++ Ven C++ 3 08-01-2006 03:56 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57