Creating Binary File Content

Discussion in 'Perl Misc' started by aisarosenbaum@yahoo.com, Feb 2, 2006.

  1. Guest

    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
     
    , Feb 2, 2006
    #1
    1. Advertising

  2. Guest

    wrote:
    > 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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Feb 2, 2006
    #2
    1. Advertising

  3. wrote:
    > 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
    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
     
    Josef Moellers, Feb 3, 2006
    #3
  4. John Bokma Guest

    wrote:

    > 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.

    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    I ploink googlegroups.com :)
     
    John Bokma, Feb 3, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neo
    Replies:
    0
    Views:
    484
  2. Replies:
    2
    Views:
    2,752
  3. hazz
    Replies:
    6
    Views:
    50,057
    SkyUCHC
    Jun 9, 2010
  4. Ron Eggler

    writing binary file (ios::binary)

    Ron Eggler, Apr 25, 2008, in forum: C++
    Replies:
    9
    Views:
    969
    James Kanze
    Apr 28, 2008
  5. Jim
    Replies:
    6
    Views:
    768
Loading...

Share This Page