unpack and endianness

42 views
Skip to first unread message

Larry Wall

unread,
Oct 4, 1990, 6:11:47 PM10/4/90
to
In article <THOTH.90O...@reef.cis.ufl.edu> th...@reef.cis.ufl.edu (Gilligan) writes:
:
: Is there any way to specify the endiannes of the data you are
: unpacking? I am writing a perl script to decode Moria save files (for
: unspecified purposes :) and unfortunately for me the data is written
: in Intel order (LSB first). I can not use unpack (as far as I know)
: on the data from our Suns (MSB first). If I could get unpack to
: perform the byte shuffling for me it would squish my code by a factor
: of two.
: Does anyone know how to do this?
:
: Could it be as simple as
:
: read(STDIN, $_, 2*2);
: ($exp, $maxexp) = unpack("s-2", $_);

There's no way to do this with unpack on a big-endian machine at the moment.
The best you can do, as far as I can see, is to say

sub swab {
$_[0] =~ s/([\0-\377])([\0-\377])/$2$1/g;
}

read(STDIN, $_, 2*2);
&swab($_);
($exp, $maxexp) = unpack("s2", $_);

: Also, what is the filehandle for <>, I hate having to type
: "moriainterp.perl < moria.dc".

It's ARGV, but you can't do a read() on it and keep the other semantics of <>.
You want something more like open(STDIN,shift).

Larry

Cameron Simpson,foo

unread,
Oct 6, 1990, 8:25:00 AM10/6/90
to
From article <98...@jpl-devvax.JPL.NASA.GOV>, by lw...@jpl-devvax.JPL.NASA.GOV (Larry Wall):

| In article <THOTH.90O...@reef.cis.ufl.edu> th...@reef.cis.ufl.edu (Gilligan) writes:
| :Is there any way to specify the endiannes of the data you are unpacking?
|
| There's no way to do this with unpack on a big-endian machine at the moment.
| The best you can do, as far as I can see, is to say
| sub swab {
| $_[0] =~ s/([\0-\377])([\0-\377])/$2$1/g;
| }
[...]

Um, why can't I write
sub swab {
$_[0] =~ s/(.)(.)/$2$1/g;
}

- Cameron Simpson, cam...@spectrum.cs.unsw.oz.au

Larry Wall

unread,
Oct 6, 1990, 11:09:54 PM10/6/90
to
In article <8...@usage.csd.unsw.oz.au> cam...@spectrum.cs.unsw.oz.au (Cameron Simpson) writes:
: From article <98...@jpl-devvax.JPL.NASA.GOV>, by lw...@jpl-devvax.JPL.NASA.GOV (Larry Wall):
: | sub swab {

: | $_[0] =~ s/([\0-\377])([\0-\377])/$2$1/g;
: | }
: [...]
:
: Um, why can't I write
: sub swab {
: $_[0] =~ s/(.)(.)/$2$1/g;
: }

Because . doesn't match \n. [\0-\377] is the most efficient way to match
everything currently. Maybe \e should match everything.

And \E would of course match nothing. :-)

Larry

Cameron Simpson,foo

unread,
Oct 7, 1990, 2:24:49 AM10/7/90
to
From article <98...@jpl-devvax.JPL.NASA.GOV>, by lw...@jpl-devvax.JPL.NASA.GOV (Larry Wall):
| In article <8...@usage.csd.unsw.oz.au> cam...@spectrum.cs.unsw.oz.au (Cameron Simpson) writes:
| : From article <98...@jpl-devvax.JPL.NASA.GOV>, by lw...@jpl-devvax.JPL.NASA.GOV (Larry Wall):
| : | $_[0] =~ s/([\0-\377])([\0-\377])/$2$1/g;
| :
| : Um, why can't I write
| : $_[0] =~ s/(.)(.)/$2$1/g;
|
| Because . doesn't match \n.

Goes back and Rs the FM more closely. Oh the embarrassment.

| [\0-\377] is the most efficient way to match
| everything currently. Maybe \e should match everything.

Nah. What's wrong with [^]?

| And \E would of course match nothing. :-)

Correspondingly, []. Haven't tried it yet (does so). Ouch. The ] at the
start of the pattern is taken as part of the range. A feature. Well, it
was a nice idea...
- Cameron Simpson
cam...@spectrum.cs.unsw.oz.au
"To every problem there is a simple, obvious, wrong solution."

Dan Bernstein

unread,
Oct 7, 1990, 9:18:25 PM10/7/90
to
In article <8...@usage.csd.unsw.oz.au> cam...@spectrum.cs.unsw.oz.au (Cameron Simpson) writes:
> From article <98...@jpl-devvax.JPL.NASA.GOV>, by lw...@jpl-devvax.JPL.NASA.GOV (Larry Wall):
> | [\0-\377] is the most efficient way to match
> | everything currently. Maybe \e should match everything.
> Nah. What's wrong with [^]?

How do you specify ] in a [^...] expression, then? You mention the same
problem with []...

---Dan

Dale Worley

unread,
Oct 8, 1990, 11:28:01 AM10/8/90
to

X-Name: Cameron Simpson,foo

Um, why can't I write

sub swab {
$_[0] =~ s/(.)(.)/$2$1/g;
}

Basically, because it doesn't work. This swaps every pair of bytes,
but doesn't swap the byte pairs, etc. The goal is to turn ABCD into
DCBA.

Dale Worley Compass, Inc. wor...@compass.com
--
The great unsolved problem of feminsm is to decide whether we need to
make men more like women, or women more like men.

Dale Lancaster

unread,
Oct 9, 1990, 1:20:43 PM10/9/90
to

>Because . doesn't match \n. [\0-\377] is the most efficient way to match
>everything currently. Maybe \e should match everything.

>And \E would of course match nothing. :-)

>Larry

I would rather see \* match everything and \e be ESC as it is in
other utilities. And maybe \!* matches nothing? :-)

dml

Mark D. Baushke

unread,
Oct 10, 1990, 11:28:56 AM10/10/90
to
On 9 Oct 90 17:20:43 GMT, da...@convex.com (Dale Lancaster) said:

Dale> In <98...@jpl-devvax.JPL.NASA.GOV> lw...@jpl-devvax.JPL.NASA.GOV
Dale> (Larry Wall) writes:

>Because . doesn't match \n. [\0-\377] is the most efficient way to match
>everything currently. Maybe \e should match everything.

>And \E would of course match nothing. :-)

>Larry

Dale> I would rather see \* match everything and \e be ESC as it is in
Dale> other utilities. And maybe \!* matches nothing? :-)

Dale> dml

Please do not create any non-alphanumeric metacharacters. It would
break the quoting of a pattern that might contain metacharacters using
$pattern =~ s/(\W)/\\$1/g;
--
Mark D. Baushke
m...@ESD.3Com.COM

Cimarron D. Taylor </>

unread,
Oct 10, 1990, 4:11:41 AM10/10/90
to

| From: da...@convex.com (Dale Lancaster)
| Newsgroups: comp.lang.perl
| Subject: Re: unpack and endianness
| Date: 9 Oct 90 17:20:43 GMT

|
| I would rather see \* match everything and \e be ESC as it is in
| other utilities. And maybe \!* matches nothing? :-)
|
| dml

But doesn't \* already mean "turn off the special meaning of *".
i.e. match only an asterix?

Cimarron Taylor
Electronics Research Laboratory / POSTGRES project
University of California, Berkeley
cima...@postgres.berkeley.edu

Larry Wall

unread,
Oct 10, 1990, 12:27:28 PM10/10/90
to
In article <MDB.90Oc...@kosciusko.ESD.3Com.COM> m...@ESD.3Com.COM (Mark D. Baushke) writes:

'Sides, there's millions of scripts out there that already use \* to mean
a literal *.

And \e doesn't mean ESC to me, it means \. What utilities does it mean
ESC in? /etc/termcap uses \E, which is close.

I HAVE considered making \a mean \007 (since K&R2 has it), but there's
gotta be a limit somewhere.

[Strange, that never stopped you before.]

Oh, shaddap!

Larry

Eric Peterson

unread,
Oct 10, 1990, 2:48:21 PM10/10/90
to
lw...@jpl-devvax.JPL.NASA.GOV (Larry Wall) writes:

| And \e doesn't mean ESC to me, it means \. What utilities does it mean
| ESC in? /etc/termcap uses \E, which is close.

Well, GNU Emacs does this, if you can call something its size a
"utility" :-)

Eric
--
Eric Peterson <> epet...@encore.com <> uunet!encore!epeterson
Encore Computer Corp. * Ft. Lauderdale, Florida * (305) 587-2900 x 5208
Why did Constantinople get the works? Gung'f abobql'f ohfvarff ohg gur Ghexf.

Dan Mick

unread,
Oct 11, 1990, 2:23:52 PM10/11/90
to
In article <CIMARRON.90...@erewhon.postgres.Berkeley.EDU> cima...@erewhon.postgres.Berkeley.EDU (Cimarron D. Taylor </>) writes:
> But doesn't \* already mean "turn off the special meaning of *".
> i.e. match only an asterix?

What's an asterix? Why would you want perl to match a cartoon character?

Reply all
Reply to author
Forward
0 new messages