Thanks,
I am going to try to read through that code tomorrow. It looks interesting.

--- Original Message ---

From: "Raul Miller" <[email protected]>
Sent: January 24, 2015 1:00 AM
To: "Programming forum" <[email protected]>
Subject: Re: [Jprogramming] J Equivalent of Python Struct.Pack/Unpack

Here is some example code I have used to extract from streamed object files:

FILEBUF=: '' NB. is read or mapped from file
POS=: 0      NB. marks next unread byte

defRD=: 2 :0
  ('read_',m)=: v
)

NB. readers - left argument of dyad is vector length to read

'char' defRD (3 :0)
  (POS=:POS+1) ] FILEBUF{~POS
:
  (POS=:POS+x) ] FILEBUF{~POS+i.x
)
'byte' defRD (a.i.read_char)

ic=: 3!:4
'Flags' defRD (3 :0)
  (POS=:POS+2) ] {. 0 ic FILEBUF {~ POS+i.2
:
  (POS=:POS+2*x) ] 0 ic FILEBUF {~ POS+i.2*x
)


fc=: 3!:5
'float' defRD  (3 :0)
  (POS=:POS+4) ] {. _1 fc FILEBUF {~ POS+i.4
:
  (POS=:POS+4*x) ] _1 fc FILEBUF {~ POS+i.4*x
)

'IndexString' defRD [: NB. don't ask

'int' defRD (3 :0)
  (POS=:POS+4) ] {. _2 ic FILEBUF {~ POS+i.4
:
  (POS=:POS+4*x) ] _2 ic FILEBUF {~ POS+i.4*x
)

NB. bools have a dual existence:
NB. a true/false value (a count - 0 or 1 times)
NB. a literal value (an arbitrary number)
'bool' defRD ((,"0~*)@read_int)

'Ptr' defRD read_int

'Ref' defRD read_int

'short' defRD (3 :0)
  (POS=:POS+2) ] {. _1 ic FILEBUF {~ POS+i.2
:
  (POS=:POS+2*x) ] _1 ic FILEBUF {~ POS+i.2*x
)

'ushort' defRD (3 :0)
  (POS=:POS+2) ] {. 0 ic FILEBUF {~ POS+i.2
:
  (POS=:POS+2*x) ] 0 ic FILEBUF {~ POS+i.2*x
)

'unsigned' defRD ((2^32) | read_int)
'uint' defRD read_unsigned

NB. special compound (or other) defs
'SizedString' defRD (3 :0)
  length=. read_int ''
  value=. length read_char ''
:
  NB. boxing needed to preserve length of strings
  x <@read_SizedString Repeated ''
)
'string' defRD read_SizedString

...

As you can see, this is rather bulky (for J). The full code is even
bulkier (and never went anywhere, though maybe I'll get back to it at
some point).

This winds up being a line of code for every item that you want to
read. But once you have defined a word that reads a structure from
file you can just use it and not worry too much about its
implementation (as long as the implementation is correct).

The code to write things back out winds up being about the same amount of bulk.

I'm not sure if this is useful to you, but I'm not using it right now,
so if you get some use from it that means my time writing it was not
entirely wasted.

Thanks,

--
Raul


On Tue, Jan 20, 2015 at 10:42 AM, Jon Hough <[email protected]> wrote:
> Python has a useful module called struct
> https://docs.python.org/2/library/struct.html
> which is useful for converting strings to packed binary data.
> e.g.
> data = struct.pack('B', someHexString)
>
> Does J have an equivalent or similar function / verb?
> I spent a bit of time looking, but couldn't find anything.
>
> Example usage would be sending data over a network.
>
>
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to