[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

background on String#unpack?

Giles Bowkett

10/26/2007 6:02:00 PM

I'm just looking for historical context on this method. It looks like
one of those old-school Unix-isms that must have been incredibly
useful in the past but never comes up in the course of run-of-the-mill
Web programming. I'm pretty sure I first saw it ten years ago in Perl
and never once used it myself. I think I did copy/paste it in some
encryption code or something related, but I'm not sure. Just
wondering, what's it for? Who uses it, and why?

--
Giles Bowkett

Blog: http://gilesbowkett.bl...
Portfolio: http://www.gilesg...
Tumblelog: http://giles.t...

10 Answers

Joel VanderWerf

10/26/2007 6:10:00 PM

0

Giles Bowkett wrote:
> I'm just looking for historical context on this method. It looks like
> one of those old-school Unix-isms that must have been incredibly
> useful in the past but never comes up in the course of run-of-the-mill
> Web programming. I'm pretty sure I first saw it ten years ago in Perl
> and never once used it myself. I think I did copy/paste it in some
> encryption code or something related, but I'm not sure. Just
> wondering, what's it for? Who uses it, and why?

Array#pack and String#unpack are useful for handling _binary_ network
protocols and file formats.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Giles Bowkett

10/26/2007 6:22:00 PM

0

> > encryption code or something related, but I'm not sure. Just
> > wondering, what's it for? Who uses it, and why?
>
> Array#pack and String#unpack are useful for handling _binary_ network
> protocols and file formats.

So would this be accurate? Use cases would include implementing
graphics formats and extracting text from proprietary-format word
processor files?

--
Giles Bowkett

Blog: http://gilesbowkett.bl...
Portfolio: http://www.gilesg...
Tumblelog: http://giles.t...

James Gray

10/26/2007 6:28:00 PM

0

On Oct 26, 2007, at 1:22 PM, Giles Bowkett wrote:

>>> encryption code or something related, but I'm not sure. Just
>>> wondering, what's it for? Who uses it, and why?
>>
>> Array#pack and String#unpack are useful for handling _binary_ network
>> protocols and file formats.
>
> So would this be accurate? Use cases would include implementing
> graphics formats and extracting text from proprietary-format word
> processor files?

Right. I wrote about that some in the summary to this quiz:

http://www.rubyquiz.com/qu...

James Edward Gray II

Rick DeNatale

10/26/2007 6:33:00 PM

0

On 10/26/07, Giles Bowkett <gilesb@gmail.com> wrote:
> I'm just looking for historical context on this method. It looks like
> one of those old-school Unix-isms that must have been incredibly
> useful in the past but never comes up in the course of run-of-the-mill
> Web programming. I'm pretty sure I first saw it ten years ago in Perl
> and never once used it myself. I think I did copy/paste it in some
> encryption code or something related, but I'm not sure. Just
> wondering, what's it for? Who uses it, and why?

String#unpack, along with its partner Array#pack is used for dealing
with binary input and output streams respectively.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Thomas Adam

10/26/2007 6:40:00 PM

0

On 26/10/2007, Giles Bowkett <gilesb@gmail.com> wrote:
> > > encryption code or something related, but I'm not sure. Just
> > > wondering, what's it for? Who uses it, and why?
> >
> > Array#pack and String#unpack are useful for handling _binary_ network
> > protocols and file formats.
>
> So would this be accurate? Use cases would include implementing
> graphics formats and extracting text from proprietary-format word
> processor files?

Anything that's encoded (URL-encoding, for instance), binary encoding.
Binary data sent down from pipes/sockets, etc.

Just look at the formatting options for it.

-- Thomas Adam

Tim Hunter

10/26/2007 6:43:00 PM

0

Giles Bowkett wrote:
>> > encryption code or something related, but I'm not sure. Just
>> > wondering, what's it for? Who uses it, and why?
>>
>> Array#pack and String#unpack are useful for handling _binary_ network
>> protocols and file formats.
>
> So would this be accurate? Use cases would include implementing
> graphics formats and extracting text from proprietary-format word
> processor files?
>

I had occasion to use these methods in code dealing with IPTC (image)
metadata.

--
Posted via http://www.ruby-....

Giles Bowkett

10/26/2007 6:55:00 PM

0

> > So would this be accurate? Use cases would include implementing
> > graphics formats and extracting text from proprietary-format word
> > processor files?
>
> Anything that's encoded (URL-encoding, for instance), binary encoding.
> Binary data sent down from pipes/sockets, etc.
>
> Just look at the formatting options for it.

I think that's one of the times I've used it. URL-decoding in Perl
before the days of CGI.pm. I think I've seen it or used it in
JavaScript that way as well, actually, for URLs.

--
Giles Bowkett

Blog: http://gilesbowkett.bl...
Portfolio: http://www.gilesg...
Tumblelog: http://giles.t...

Giles Bowkett

10/26/2007 10:13:00 PM

0

> Here's a real world example where I am trying to figure out how to use
> string#unpack. I'm using the ruby-net-ldap library to pull info from Active
> Directory. One of the fields, objectguid, comes back as a binary field. I
> need to be able to compare it to (I think, I'm no expert here) what looks
> like a hexidecimal representation of this field. So, I'm trying to figure
> out how to convert this binary field I get back from Active Directory, into
> a hexidecimal field. I think I need to use the unpack method, but I haven't
> figured out what format string to pass the method yet.
>
> Didn't mean to hijack the thread, just thought it kind of tied in with what
> you were asking.

Here's a Perl snippet that seems to do it:

$binary = unpack("B32", pack("N", hex($hex)));

presumably the Ruby equivalent would be similar. although actually I
think this converts hex to binary instead of converting binary to hex.

There's a Perl tutorial on it
(http://perldoc.perl.org/perlpa...) but I don't know how much
the syntax for pack/unpack statements in Perl and Ruby conform or
differ. Seems like an old-school Unix DSL, like regexen.

--
Giles Bowkett

Blog: http://gilesbowkett.bl...
Portfolio: http://www.gilesg...
Tumblelog: http://giles.t...

Stefan Rusterholz

10/26/2007 10:22:00 PM

0

Giles Bowkett wrote:
> I'm just looking for historical context on this method. It looks like
> one of those old-school Unix-isms that must have been incredibly
> useful in the past but never comes up in the course of run-of-the-mill
> Web programming. I'm pretty sure I first saw it ten years ago in Perl
> and never once used it myself. I think I did copy/paste it in some
> encryption code or something related, but I'm not sure. Just
> wondering, what's it for? Who uses it, and why?
>
> --
> Giles Bowkett
>
> Blog: http://gilesbowkett.bl...
> Portfolio: http://www.gilesg...
> Tumblelog: http://giles.t...

Since it came up here and is somewhat related: I recently wrote out of
fun some wrapper methods to make it a bit nicer to read/write binary
datastructures. IMHO it's a bit cumbersome to do things like:
size = socket.read(4).unpack("I").first
meta = SomeStruct.new(*socket.read(x).unpack(format))
data = socket.read(size)

The wrappers are attached and here: http://pastie.caboo...
Use it at your own risk ;-)

Regards
Stefan

Attachments:
http://www.ruby-...attachment/790...

--
Posted via http://www.ruby-....

Joel VanderWerf

10/26/2007 10:47:00 PM

0

Stefan Rusterholz wrote:
> Since it came up here and is somewhat related: I recently wrote out of
> fun some wrapper methods to make it a bit nicer to read/write binary
> datastructures. IMHO it's a bit cumbersome to do things like:
> size = socket.read(4).unpack("I").first
> meta = SomeStruct.new(*socket.read(x).unpack(format))
> data = socket.read(size)

See also:

binaryparse
bindata
bitstruct

The first two are gems, the last is tgz, available at
http://redshift.sourceforge.net/... (that one's mine).

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407