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?
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.
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.
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.
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
(perlpacktut - tutorial on pack and unpack - Perldoc Browser) 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.
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?
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: Parked at Loopia
Use it at your own risk
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.
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 BitStruct (that one’s mine).
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.