Perhaps this should be a new subject like "adding formats for
pack/unpack". I gave this a bit of thought, and there are quite a few
formats that seem to be missing. I would naturally assume that there
should be support for each combination of

{unsigned, signed} x {native, network, little-endian} x {short, int,
long}

However, of these 18 formats, 8 are missing. There are more than enough
unused characters to specify them (j J k K o O r R t T W y Y z). I
tried to come up with consistent mappings based on what already exists
and this is what I came up with (?s indicate that the format is not
currently supported):

S	unsigned native short
s	signed native short
n	unsigned network short
o ?	signed network short
v	unsigned little-endian short
r ?	signed little-endian short

I	unsigned native int
i	signed native int
J ?	unsigned network int
j ?	signed network int
K ?	unsigned little-endian int
k ?	signed little-endian int

L	unsigned native long
l	signed native long
N	unsigned network long
y ?	signed network long
V	unsigned little-endian long
y ?	signed little-endian long

I'm thinking of submitting an RCR for this, but before I go through the
effort of figuring out the c code to achieve it, I wanted some opinions
as to whether this is worthwhile and whether the mappings are sensible.

Thanks.

Joel VanderWerf wrote:
> baumanj / gmail.com wrote:
> > Is there any particular reason why there is no format specifier in the
> > String.unpack method for unpacking a signed short in network byte
> > order? There seems only to be one for unsigned, which can then be
> > converted to signed with a proc like this:
> >
> > length = 16
> > max = 2**length-1
> > mid = 2**(length-1)
> > to_signed = proc {|n| (n >= mid) ? -((n ^ max) + 1) : n}
> >
> > It seems reasonable to add a format specifier to do this directly.
> > Thoughts?
> >
>
> I'd vote for that. Looks like you already found this thread :)
>
> http://blade.nagaokaut.ac.jp/cgi-bin/vframe.rb/ruby/ruby-talk/165774?165771-168359
>
>
> -- 
>       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407