Feature #3491: Pack missing directives for signed types with specified byte-order
http://redmine.ruby-lang.org/issues/show/3491

Author: Erik M
Status: Open, Priority: Normal
Category: core

The Array#pack and String#unpack have some directives where the byte-order is specified ("little endian" or "network order"), for unsigned long and for unsigned short. But when it comes to signed long and signed short there is no way to specify byte order, you can only use the platform-dependent directives.

This issue was mentioned on a mailing list in 2006: http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/186884
and from what I understood it is a bug inherited from the pack and unpack functions in Perl. The conversation ended by matz having talked to the Perl lead programmer about coordination so both languages solve this issue the same way. But then I don't know if anything happened.

___A possible solution___
http://perldoc.perl.org/perlpacktut.html#Byte-order-modifiers mentions Perl 5.9.2 supporting ">" and "<" to specify byte order for the directives that usually use the platform-dependent order. That seems like a solution! The documentation at http://search.cpan.org/~dapm/perl-5.10.1/pod/perlfunc.pod#pack may be easier to read.

___More about current status___
The docs for Ruby 1.9 still seem to miss this feature. Also the 'N' and 'V' directives operate on unsigned longs, which I think is not clear from the documentation. For all the platform-dependent formats cCsSiIlL it has pairs like "Unsigned long" and "Long" (which is then signed), but for the platform-indepedent suddenly "Long" means "Unsigned long" (applies to formats nNvV). I think that is confusing.

As explained in http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/165961 it is possible to work around this issue by unpacking with the unsigned directive and then making a function that can convert from unsigned to signed. For Array#pack it seems the function doesn't care if you input signed values, the same directive works for unsigned and signed data when packing.

I'm not sure if this is a bug or a feature request, but to be polite it made it a feature. I tested on ruby 1.8.7 (2010-01-10 patchlevel 249) [i486-linux].


----------------------------------------
http://redmine.ruby-lang.org