BInary Files

G

Gene Vincent

Hi I'm pretty new to Ruby and writing code in general. That being said
Im trying to learn how to create and modify(by a percentage) a binary
file. Any advise or pointers would be apprieciated.
 
M

Michael Malone

Gene said:
Hi I'm pretty new to Ruby and writing code in general. That being said
Im trying to learn how to create and modify(by a percentage) a binary
file. Any advise or pointers would be apprieciated.
My personal view, is that binary files should be avoided unless
absolutely necessary. Being able to look through the contents of a file
and eyeball what's wrong is incredibly useful. That being said, if you
absolutely need to write binary, take a look at the documentation for
the Array#pack method. That should give you a nice starting point.

Michael

=======================================================================
This email, including any attachments, is only for the intended
addressee. It is subject to copyright, is confidential and may be
the subject of legal or other privilege, none of which is waived or
lost by reason of this transmission.
If the receiver is not the intended addressee, please accept our
apologies, notify us by return, delete all copies and perform no
other act on the email.
Unfortunately, we cannot warrant that the email has not been
altered or corrupted during transmission.
=======================================================================
 
G

Gene Vincent

Michael said:
My personal view, is that binary files should be avoided unless
absolutely necessary. Being able to look through the contents of a file
and eyeball what's wrong is incredibly useful. That being said, if you
absolutely need to write binary, take a look at the documentation for
the Array#pack method. That should give you a nice starting point.

Michael

=======================================================================
This email, including any attachments, is only for the intended
addressee. It is subject to copyright, is confidential and may be
the subject of legal or other privilege, none of which is waived or
lost by reason of this transmission.
If the receiver is not the intended addressee, please accept our
apologies, notify us by return, delete all copies and perform no
other act on the email.
Unfortunately, we cannot warrant that the email has not been
altered or corrupted during transmission.
=======================================================================

sadly yes I do have to use some binary files for the test im trying to
run, but thank you very much for the info.
Gene
 
M

Michael Bruschkewitz

Joel VanderWerf said:
what would that do?

Writing output bit-wise to a (ex.) string. Read back.
Convert to/from char-array.

Example:
Given:
a,b,c,d values of different bit-size
a is a flag is 1 bit long,
b is 5 bit long, c is 7 bit long.
d is 3 bit long.
It should be possible:
bs << a << (1==a ? b : c) << d
bs >> a >> (1==a ? b : c) >> d

After insertion, bs should contain following bits (p is padding):
1bbb bbdd dppp pppp
or
0ccc cccc dddp pppp.

Other example would be var-length array of 5-bit values followed by
var-length array of 13-bit structs.

Use case: ASN.1 and similar formats.
 
J

Joel VanderWerf

It would be nice for ruby to have some bit operation primitives like
extract(start_bit, n_bits) and insert and so on. Then bit stream ops
could be built op from these. The former would make a nice C ext...
 
M

Michael Bruschkewitz

Joel VanderWerf said:
It would be nice for ruby to have some bit operation primitives like
extract(start_bit, n_bits) and insert and so on. Then bit stream ops
could be built op from these. The former would make a nice C ext...

I did some rudimentary work (enough for my purposes) here:
http://www.bruschkewitz.de/job/ruby/CBitStream.rb

There is no optimisation at all, just made it work.

I would prefer not to use a C-Extension unless it would be part of Ruby
standard library.

IMHO, there are enough possibilities to optimize in Ruby.
For example, it is not necessary to analyze the values bit by bit.
Instead, if the current buffer byte contains already 1 bit from former
output operations, remaining 7 bits, these 7 bits could be copied at once.
It breaks down to shift the value to be written by some bits (if even
necessary), "or"-ing current buffer byte and assinging rest of output bytes
to buffer.
I did similar work some years before in Modula.
Depending on output buffer representation (byte, word, dword, qword, Array,
String, Bignum), this could be very effective even in Ruby.

So, fitting into the general performance landscape, an implementation done
in Ruby would be enough, because the values to be written will - most
probably - be prepared in Ruby too.


Regards,
Michael B.
 

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,780
Messages
2,569,611
Members
45,273
Latest member
DamonShoem

Latest Threads

Top