open a binary file-read bit-change bit-update same file

Discussion in 'Ruby' started by Sniper Abandon, Nov 19, 2009.

  1. my Aim is i want to encrypt a file

    i want to open a binary file

    i want to read the 1-st bit
    if the bit is "1" then update that position bit to "0" instantly
    if the bit is "0" then update that position bit to "1" instantly

    then i have to read 2-nd bit
    do the same thing like above conversion
    .......
    .......
    like i want to do this for all bit of that file

    ---------------------


    i know we can read the file into a array then we can do the
    modification,etc
    but i want to update on that same file object, and the file size may be
    more then 1 GB


    is it possible?
    any idea and help is very much appreciated
    --
    Posted via http://www.ruby-forum.com/.
     
    Sniper Abandon, Nov 19, 2009
    #1
    1. Advertising

  2. 2009/11/19 Sniper Abandon <>:
    > my Aim is i want to encrypt a file
    >
    > i want to open a binary file
    >
    > i want to read the 1-st bit
    > if the bit is "1" then update that position bit to "0" instantly
    > if the bit is "0" then update that position bit to "1" instantly
    >
    > then i have to read 2-nd bit
    > do the same thing like above conversion
    > .......
    > .......
    > like i want to do this for all bit of that file
    >
    > ---------------------
    >
    >
    > i know we can read the file into a array then we can do the
    > modification,etc
    > but i want to update on that same file object, and the file size may be
    > more then 1 GB
    >
    > is it possible?


    Anything is possible. But doing it bitwise is extremely inefficient.
    You should rather do it in chunks of, say, 1025 or 512 bytes or - even
    better - use the cluster size of your file system.

    XOR will be useful

    irb(main):004:0> a = 11
    => 11
    irb(main):005:0> sprintf "%08b -> %08b", a, a ^ 0xFF
    => "00001011 -> 11110100"


    Kind regards

    robert


    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Nov 19, 2009
    #2
    1. Advertising

  3. Sniper Abandon

    Dalthon [BR] Guest

    You can synchronize io setting file.sync = true to update on the same
    file object

    On 19 nov, 07:20, Robert Klemme <> wrote:
    > 2009/11/19 Sniper Abandon <>:
    >
    >
    >
    > > my Aim is i want to encrypt a file

    >
    > > i want to open a binary file

    >
    > > i want to read the 1-st bit
    > > if the bit is "1" then update that position bit to "0" instantly
    > > if the bit is "0" then update that position bit to "1" instantly

    >
    > > then i have to read 2-nd bit
    > > do the same thing like above conversion
    > > .......
    > > .......
    > > like i want to do this for all bit of that file

    >
    > > ---------------------

    >
    > > i know we can read the file into a array then we can do the
    > > modification,etc
    > > but i want to update on that same file object, and the file size may be
    > > more then 1 GB

    >
    > > is it possible?

    >
    > Anything is possible.  But doing it bitwise is extremely inefficient.
    > You should rather do it in chunks of, say, 1025 or 512 bytes or - even
    > better - use the cluster size of your file system.
    >
    > XOR will be useful
    >
    > irb(main):004:0> a = 11
    > => 11
    > irb(main):005:0> sprintf "%08b -> %08b", a, a ^ 0xFF
    > => "00001011 -> 11110100"
    >
    > Kind regards
    >
    > robert
    >
    > --
    > remember.guy do |as, often| as.you_can - without endhttp://blog.rubybestpractices.com/
     
    Dalthon [BR], Nov 19, 2009
    #3
  4. 2009/11/19 Dalthon [BR] <>:
    > You can synchronize io setting file.sync = true to update on the same
    > file object


    That does only change the point in time _when_ the change in the file
    can be observed by other processes but it does not affect _where_ the
    changes are written. For that (i.e. writing to the same file) you
    need File.open(name, "r+") anyway.

    If you use that option and do "bitwise" updates (actually only
    bytewise is possible) efficiency will drop even more dramatically.

    Kind regards

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Nov 19, 2009
    #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. Fangs
    Replies:
    3
    Views:
    9,839
    darshana
    Oct 26, 2008
  2. Replies:
    12
    Views:
    595
    Richard Heathfield
    Apr 8, 2007
  3. Replies:
    5
    Views:
    515
    Colin Song
    Oct 4, 2007
  4. harijay
    Replies:
    2
    Views:
    492
    Gabriel Genellina
    Feb 19, 2009
  5. Replies:
    7
    Views:
    243
    Bob Barrows [MVP]
    Nov 11, 2005
Loading...

Share This Page