[Jprogramming] Binary structure conversion
Dan Bron
j at bron.us
Sun Dec 23 00:37:56 HKT 2007
Oleg wrote:
> I am thinking to further generalize the approach and simplify
> the interface, in the way pack/unpack work in Perl
> I was wondering if someone already done it or at least
> thought of such interface.
Rewriting pack/unpack has been on my todo list for years. I have thought about it many times. But the only code I have is
several useless stubs.
The way I thought about it is you'd have an adverb unpack which took a Perl-style unpack() template. The adverb, given the
template, derives a monad. The monad, given a list of bytes (chars), derives a list of boxes. Each box contains a list of the
values of its corresponding template phrase.
So basically, the template defines a single message with multiple values. The verb cuts its stream into messages, and each
message into values, converting them accordingly. Then its result is a list of boxes, each containing its corresponding values,
one per message.
On top of that, you could have a script which took such a list of boxes, derived a object, and assigned names to the contents of
the boxes.
I've also considered extending the idea to allow other kinds of templates. C structs for example. Or maybe XML descriptions of
data, like BFD: http://collaboratory.emsl.pnl.gov/sam/bfd/
-Dan
PS: Other links mentioning Perl's pack():
Raul:
http://www.jsoftware.com/pipermail/general/2005-October/024741.html
Me:
http://www.jsoftware.com/pipermail/programming/2007-June/007143.html
I also made a pack() request in Oct 2004, but it didn't make it into the archive. It is appended.
> -----Original Message-----
> From: Daniel Bron [mailto:DanielB at MAPLEUSA.COM]
> Sent: Friday, October 29, 2004 11:46 AM
> To: JFORUM at PEACH.EASE.LSOFT.COM
> Subject: [Jforum] Utility similar to Perl's pack()
>
> Forum,
>
> Does anyone have a utility similar to Perl's pack()? To
> quote from the Perl documentation:
>
> ------------------
> pack TEMPLATE,LIST
>
> Takes a LIST of values and converts it into a string using
> the rules given by the TEMPLATE. The resulting string is the
> concatenation of the converted values. Typically, each
> converted value looks like its machine-level representation.
> For example, on 32-bit machines a converted integer may be
> represented by a sequence of 4 bytes.
>
> The TEMPLATE is a sequence of characters that give the order
> and type of values, as follows:
> ...
> ------------------
>
> For a fuller description, see
> http://www.perldoc.com/perl5.8.4/pod/func/pack.html
>
> Basically, I want to stream binary data over a socket, and
> the data is in a specific format, e.g. a int followed by an
> unsigned short, two doubles, and a null-terminated (C) string.
>
> I'd like to give a template string and list of boxes
> containing J-type values as input and get a string encoding
> (as specified by the template) the J-values as output.
> Either a dyad or an adverb would be acceptable.
>
> -Dan
>
> --------------------------------------------------------------
> ------------
> For information about the J Forum see
> http://www.jsoftware.com/j_forum.htm
>
More information about the Programming
mailing list