how do I convert a file into its 8 bit 0/1 pattern ?

J

Jack

Hi,

I assume to do this I would need to:

1- read in a file and store its binary (byte) contents into an array (in increments)
2- convert the binary bytes into their 8 bit 0/1 equivalents

Does anyone happen to have the syntax to accomplish this ?

Thank you,

Jack
 
A

Anno Siegel

Jack said:
Hi,

I assume to do this I would need to:

1- read in a file and store its binary (byte) contents into an array (in
increments)
2- convert the binary bytes into their 8 bit 0/1 equivalents

Does anyone happen to have the syntax to accomplish this ?

The syntax is that of "open", "read" and "sprintf".

Anno
 
B

Brian McCauley

1- read in a file and store its binary (byte) contents into an array (in increments)
2- convert the binary bytes into their 8 bit 0/1 equivalents

I do not understand what you mean by this. On a computer[1] a file
consits of bytes and those bytes consist of 8 bits.

Perhaps if you gave an example of the desired input and output?

Anyhow this smells like an XY problem. Whatever it is that you expect
to achive by converting the file into whatever form it is that you
were trying to describe there's almost certainly a more direct
approach.

[1] Well there are exceptions but you are unlikely to be in one of
those environments.

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
J

Jack

Brian McCauley said:
1- read in a file and store its binary (byte) contents into an array (in increments)
2- convert the binary bytes into their 8 bit 0/1 equivalents

I do not understand what you mean by this. On a computer[1] a file
consits of bytes and those bytes consist of 8 bits.

Perhaps if you gave an example of the desired input and output?

Anyhow this smells like an XY problem. Whatever it is that you expect
to achive by converting the file into whatever form it is that you
were trying to describe there's almost certainly a more direct
approach.

[1] Well there are exceptions but you are unlikely to be in one of
those environments.

I want to capture the exact 0/1 bitstream from a datafile. How do I
accomplish this ?

thank you,

Jack
 
B

Brian McCauley

Brian McCauley said:
1- read in a file and store its binary (byte) contents into an array (in increments)
2- convert the binary bytes into their 8 bit 0/1 equivalents

I do not understand what you mean by this. On a computer[1] a file
consits of bytes and those bytes consist of 8 bits.

Perhaps if you gave an example of the desired input and output?

I want to capture the exact 0/1 bitstream from a datafile.

That does not clarify matters at all.

I suspect that what you think you are saying here is something more
than what you are actually saying. Try to take a step back and look
at what you are saying from the point of view of someone else. For a
scarily similar example of how someone can think they are asking a
perfectly clear question when actually they are not please see:

http://groups.google.com/[email protected]
How do I accomplish this ?

Well to answer the question you ask (as opposed to the question you
are trying to ask - of which I'm still in the dark)...

open() - to open the file
binmode() - to tell perl to treat the the file as binary data
read() - to read chunks of the file into a scalar variable used as a buffer.

use constant CHUNK => 1024;
my $filename='whatever';
open(my $fh, '<', $filename) or die $!;
binmode($fh);
{
my $r = read($fh, my $buffer, CHUNK);
die $! unless defined $r;
last unless $r;
# Do stuff with $buffer;
redo;
}

Examples like this should be easily found in any Perl tutorial or reference.

Actually in recent Perl open() and binmode() can be combined. For
details RTFM on open in 5.8.

You can also use readline() (aka the <> operator) rather than read().
For details on putting readline() into fixed size record mode (or
read-the-whole-file (aka "slurp") mode) RTFM on the special variable
$/.

See also:
File::Slurp.
FAQ: "How can I manipulate fixed-record-length files?"
FAQ: "How can I read in an entire file all at once?"
perldoc -f open
perldoc -f binmode
perldoc -f read
Posting guidelines for this newsgroup.

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
J

Joe Smith

Jack said:
I want to capture the exact 0/1 bitstream from a datafile. How do I
accomplish this ?

Just read in the data and write it back out.

The data will come in as individual bytes (8 bits each) and
the exact 1/0 bitstream of 8-bit bytes will be written out.

Now, if you wish to convert a single 8-bit byte into 8 separate characters of
"1" and "0" each, that is a different problem. But you did not ask for that.
-Joe
 
R

Richard Morse

Joe Smith said:
Just read in the data and write it back out.

The data will come in as individual bytes (8 bits each) and
the exact 1/0 bitstream of 8-bit bytes will be written out.

With the caveat that on some operating systems you need to 'binmode' (or
otherwise open in binary mode) the file handles.

Ricky
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,158
Messages
2,570,882
Members
47,414
Latest member
djangoframe

Latest Threads

Top