Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

how to convert numbers to ASCII characters

by redss (Monk)
on Jan 24, 2008 at 13:59 UTC ( [id://664023]=perlquestion: print w/replies, xml ) Need Help??

redss has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I want to take a series of numbers, each number between 0 and 255, and write each number to an ASCII file as a single character.

How can I write each number to the file as an ASCII character, and then convert each character back to the number when reading from the file?

  • Comment on how to convert numbers to ASCII characters

Replies are listed 'Best First'.
Re: how to convert numbers to ASCII characters
by olus (Curate) on Jan 24, 2008 at 14:04 UTC
    Check the chr and ord functions.
    print FILE chr(78); $number = ord('N');
Re: how to convert numbers to ASCII characters
by moritz (Cardinal) on Jan 24, 2008 at 14:05 UTC
    ASCII covers only the range from 0 to 127. (And yes, chr and ord are the right builtins).
      ok that is fine if ASCII only covers 0-127. The file I write to can be a binary file rather than ascii, so will the same functions work in that case?
        Yes, they will work. Perl uses Latin1 in the range 0..255.
Re: how to convert numbers to ASCII characters
by shmem (Chancellor) on Jan 24, 2008 at 17:36 UTC
    See pack and unpack.
    print FILE pack "C*", @numbers; # write @numbers = unpack "C*", <FILE>; # read

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      Binary files aren't likely to be LF-terminated, so <...> isn't likely to be appropriate.
      sub read_block { my ($fh, $bytes_to_read) = @_; my $buf = ''; while ($bytes_to_read) { my $bytes_read = read($fh, $buf, $bytes_to_read, length($buf)); die("Unable to read from file: $!\n") if !defined($bytes_read); die("Unable to read from file: Unexpected end of file\n") if !$bytes_read; $bytes_to_read -= $bytes_read; } return $buf; } # Write print $fh (pack('C', scalar(@numbers)); print $fh (pack('C*', @numbers)); # Read my $num_numbers = unpack('C', read_block($fh, 1)); my @numbers = unpack('C*', read_block($fh, $num_numbers));

      Update: Fixed incorrect variable name. Thanks blokhead.

        Binary files aren't likely to be LF-terminated, so <...> isn't likely to be appropriate.

        As always, that depends. It is likely to be appropriate if the file isn't too big and read in slurp mode.

        --shmem

        _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                      /\_¯/(q    /
        ----------------------------  \__(m.====·.(_("always off the crowd"))."·
        ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: how to convert numbers to ASCII characters
by oxone (Friar) on Jan 24, 2008 at 16:50 UTC
    Watch out for this banana skin: you also need to 'binmode' your file when reading/writing it, otherwise characters with numbers 10 and 13 (used for line ends) may get messed up.
Re: how to convert numbers to ASCII characters
by ikegami (Patriarch) on Jan 24, 2008 at 17:38 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://664023]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-19 13:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found