Creating Binary File Content

A

aisarosenbaum

Hi,

I'm trying to create files containg random binary data. I need to
write ones and zeroes one at a time. Does anyone have any code they
can share to do this? (I'm not trying to create random ASCII data, but
raw binary data at the bit level)

I've read about pack. It doesn't appear to do what I need. Or am I
missing something?

Thanks In Advance,

Todd
 
X

xhoster

Hi,

I'm trying to create files containg random binary data.

my $size=10_000; #size in bytes
open my $fh, "/dev/random" or die $!;
read $fh, my $foo, $size;
close $fh or die $!;

open my $out, ">whatever" or die $!;
print $out $foo;
close $out or die $!;
I need to
write ones and zeroes one at a time.

You can't write one bit at a time. You have to deal in bytes (at least).
If you really need to do this, then you need to have a flag byte which
tells you how many of the bits in the last byte are significant, rather
than padding. Then you would have to constantly read in the last byte and
the flag byte, use bit logic to update both of them, and then write out
both of them.
Does anyone have any code they
can share to do this? (I'm not trying to create random ASCII data, but
raw binary data at the bit level)

I've read about pack. It doesn't appear to do what I need. Or am I
missing something?

Probably. It hard to tell what you are missing, as you didn't show us what
you tried, what you wanted it to do, and what it did instead.

Xho
 
J

Josef Moellers

Hi,

I'm trying to create files containg random binary data. I need to
write ones and zeroes one at a time. Does anyone have any code they
can share to do this? (I'm not trying to create random ASCII data, but
raw binary data at the bit level)

I've read about pack. It doesn't appear to do what I need. Or am I
missing something?

You can use 'b' or 'B' (if you want to write binary data, the order is
irrelevant, so use any one of these):

my @a = (1, 0, 0, 0, 0, 1, 1, 0);
my $s = '';
foreach (@a) {
$s .= $_;
}
my $v = pack('b*', ($s));
print $v, "\n";

You would need to switch the output file to binary mode, too:

open(my $dst, '>', "randmofile");
binmode $dst;

HTH,

Josef
 
J

John Bokma

Hi,

I'm trying to create files containg random binary data. I need to
write ones and zeroes one at a time.

I doubt you can write a file consisting of 17 bits :-D So it's always
padded to a multiple of 8 bits.

The question is, are you ok with 8 bits, or do you want to start with 8
zeroes, then with a probability of 1/2 flip the "first" bit (lsb or msb),
write, move to the next one, etc. until 8 bits are done, and then the next
byte.

otherwise, generating a byte 0..255 would do the trick.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top