CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [odubiel] help with While statement; and reading with ord: Edit Log



Kenosis
User

Mar 25, 2013, 12:39 PM


Views: 347
Re: [odubiel] help with While statement; and reading with ord

You can do the following for this:


Code
use strict; 
use warnings;

local $/ = \1;
while (<>) {
print sprintf '%08b', ord;
}


Usage: perl script.pl inFile [>outFile]

The last, optional parameter will direct output to a file.

Example inFile:


Code
ABCD


Results:


Code
01000001010000100100001101000100


Perl takes one byte at a time from the file (implicitly read by <>), since the record separator is set to \1 (see $INPUT_RECORD_SEPARATOR here), and sprintf formats the ordinal value of that byte (implicitly passed to ord in $_) as binary.

Hope this helps!


(This post was edited by Kenosis on Mar 25, 2013, 1:09 PM)


Edit Log:
Post edited by Kenosis (User) on Mar 25, 2013, 12:44 PM
Post edited by Kenosis (User) on Mar 25, 2013, 12:52 PM
Post edited by Kenosis (User) on Mar 25, 2013, 12:54 PM
Post edited by Kenosis (User) on Mar 25, 2013, 12:57 PM
Post edited by Kenosis (User) on Mar 25, 2013, 1:01 PM
Post edited by Kenosis (User) on Mar 25, 2013, 1:02 PM
Post edited by Kenosis (User) on Mar 25, 2013, 1:09 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives